开发者

PyQt4 global shortcuts?

开发者 https://www.devze.com 2023-01-05 18:57 出处:网络
I have an application that opens multiple children widgets as separate windows, something like th开发者_开发问答is: window1 opens window 2 which opens window 3 (simplified form).

I have an application that opens multiple children widgets as separate windows, something like th开发者_开发问答is: window1 opens window 2 which opens window 3 (simplified form).

In the main window I have set CTRL+Q as the quit shortcut. Below is a stripped down example of the main class.

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.actionExit = QtGui.QAction(_('E&xit'),self)
        self.actionExit.setShortcut('Ctrl+Q')
        self.actionExit.setStatusTip(_('Close application'))
        self.connect(self.actionExit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))

Right now if I open the third child and push CTRL+Q nothing happens. Is there a way so that the children inherit the shortcut key for quit or to make the shortcut global or do I have to declare it in each of them?


Here is what I have used in __init__ function: QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Q"), self, self.close)

It works smoothly!


Try setting the ShortcutContext.

self.actionExit.setShortcutContext(QtCore.Qt.ApplicationShortcut)


You can also just set a shortcut for your QAction directly:

self.actionExit.setShortcut(QtGui.QKeySequence("Ctrl+Q"))

The only difference between this example and your code is that the Ctrl+Q is first cast to QtGui.QKeySequence.

0

精彩评论

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