2017-08-10 52 views
0

我想攔截默認情況下,在任何QLineEdit的插件創建一個QLineEdit的上下文菜單中的粘貼操作(見下圖)如何處理Qt中QLineEdit上下文菜單中的動作?

enter image description here

有沒有辦法來重定向上下文的粘貼動作菜單通過任何方式?

+0

你是什麼意思重定向? ,粘貼的文字是不同的。 – eyllanesc

+0

@eyllansec我一般想攔截事件和不同的方式處理數據。 – Woltan

回答

0

人們可以通過重載QLineEdit的小部件的contextMenuEvent在上下文菜單的操作撥弄。

編輯

void LineEdit::contextMenuEvent(QContextMenuEvent *event) 
{ 
    QMenu *menu = createStandardContextMenu(); 
    menu->addAction(tr("My Menu Item")); 
    //... 
    menu->exec(event->globalPos()); 
    delete menu; 
} 

和代碼,我實際使用,我的目的:

上面的鏈接的代碼

menu = self.createStandardContextMenu() 

menu.actions()[5].connect(self.paste) # The hard ref to the 6th item is not ideal but what can you do... 

menu.exec_(event.globalPos()) 
+0

你也可以使用'createStandardContextMenu()':'無效LineEdit ::的ContextMenuEvent(QContextMenuEvent *事件) { QMenu *菜單= createStandardContextMenu(); 的QAction *行動=菜單 - > EXEC(事件 - > globalPos()); 如果(動作 - >文本()比較( 「&粘貼\ TCTRL + V」)== 0){ qDebug()<< 「測試」; } }' – eyllanesc

+0

@eyllanesc這是正確的。出於完整性,我編輯了我的答案以顯示完整的代碼。 – Woltan

相關問題