开发者

Create one keybord shortcut for 2 objects in PyQt

开发者 https://www.devze.com 2023-01-16 21:27 出处:网络
How can i create for \"Ctrl+C\" bindings for 2 objects: self.table, self.editor I have: shortcut = QtGui.QShortcut(QtGui.QKeySeque开发者_C百科nce(\"Ctrl+C\"), self.table, None, self.copyTable)

How can i create for "Ctrl+C" bindings for 2 objects: self.table, self.editor

I have:

shortcut = QtGui.QShortcut(QtGui.QKeySeque开发者_C百科nce("Ctrl+C"), self.table, None, self.copyTable)
shortcut2 = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+C"), self.editor, None, self.copyText)

This works, but is toogled. If i have focus on self.editor and for the first time i press "Ctrl+C it does self.copyTable, the second time is does self.copyText.

What am i doing wrong? :P

I did find a workaround where i create a QAction which checks which object has focus and triggers the wanted action. But i would rather have it per object.

Edit (a working example):

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


You have to set the correct context for short cuts: by default they are window-"global", you probably want them to be widget-"local". See setShortcutContext.


i did that already here and it worked fine ^_^. very simple idea .

just make one shortcut and one slot.

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

and inside copyFunction check the focus , like so :

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

精彩评论

暂无评论...
验证码 换一张
取 消