2010-09-16 36 views
0

我如何爲2個對象 「CTRL + C」 綁定創建一個快捷方式keybord:self.table,self.editor創建2個對象在PyQt的

我:

shortcut = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+C"), self.table, None, self.copyTable) 
shortcut2 = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+C"), self.editor, None, self.copyText) 

該作品,但是被迷惑了。如果我在self.editor並首次有重點我按下「Ctrl + C它self.copyTable,第二次是沒有self.copyText

我在做什麼錯:2 P

我發現在那裏我創建的QAction,檢查哪個對象具有焦點,並觸發所需動作的方法,但我寧願有它的每個對象

編輯(工作示例):

shortcut = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+C"), self, self.copytoclipbord) 
shortcut.setContext(QtCore.Qt.WidgetShortcut) 
+0

你們這一個我不接受;)做到了現在:) – hugo24 2010-09-27 05:49:01

+0

您可以通過'上下文= QtCore.Qt.WidgetShortcut ''QShortcut' init的參數 – Winand 2015-10-01 07:57:41

回答

3

你必須爲快捷方式設置正確的上下文:默認情況下,它們是window-「glob」 al「,你可能希望它們成爲widget」本地「。見setShortcutContext

3

我已經在這裏,它工作得很好^ _ ^。非常簡單的想法。

只做一個快捷方式和一個插槽。

QtGui.QShortcut(QtGui.QKeySequence("Ctrl+C"), self, None, self.copyFunction) 

和內部copyFunction檢查的重點,像這樣:

def copyFunction(self): 
    if self.table.hasFocus: 
     self.copyTable() 
    elif self.editor.hasFocus: 
     self.copyEditor()