PyQt4全局快捷键?
我有一个应用程序将多个子窗口小部件作为单独的窗口打开,如下所示:window1 打开窗口 2,窗口 2 打开窗口 3(简化形式)。
在主窗口中,我将 CTRL+Q 设置为退出快捷方式。下面是主类的精简示例。
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()'))
现在,如果我打开第三个子项并按 CTRL+Q,则什么也不会发生。有没有办法让孩子们继承退出快捷键或使快捷方式全局化,还是我必须在每个孩子中声明它?
I have an application that opens multiple children widgets as separate windows, something like this: 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我在 __init__ 函数中使用的内容:
QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Q"), self, self.close)
运行顺利!
Here is what I have used in
__init__
function:QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Q"), self, self.close)
It works smoothly!
尝试设置 ShortcutContext。
Try setting the ShortcutContext.
您还可以直接为 QAction 设置快捷方式:
此示例与您的代码之间的唯一区别是
Ctrl+Q
首先转换为QtGui.QKeySequence
。You can also just set a shortcut for your QAction directly:
The only difference between this example and your code is that the
Ctrl+Q
is first cast toQtGui.QKeySequence
.