Qt中如何控制按键事件

How to contol KeyEvent in Qt

使用 CustomWidget 声明的 window 为超级 class:class App(CustomWidget) 按 Alt+A 正确打印 'keyPressEvent: Alt + a' 消息。

但是当使用 setCentralWidget() 将 CustomWidget 分配给 window 或使用 layer.addWidget(widget) 设置时,KeyEvent 功能会被破坏。代码中缺少什么?

from PyQt4 import QtCore, QtGui

class CustomWidget(QtGui.QWidget):
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent=parent)

    def keyPressEvent(self, event):
        if event.modifiers() == QtCore.Qt.AltModifier:
            if event.key() == QtCore.Qt.Key_A:
                print 'keyPressEvent: Alt + a'
        # super(CustomWidget, self).keyPressEvent(event)

class App(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent=parent) 
        centralWidget = CustomWidget(self) 

        self.setCentralWidget(centralWidget)

        mainLayout=QtGui.QVBoxLayout()
        centralWidget.setLayout(mainLayout)

        widget = CustomWidget(self)
        mainLayout.addWidget(widget)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = App()
    w.show()
    sys.exit(app.exec_())

小部件必须有焦点才能接收事件。确保在创建 window.

后调用 setFocusPolicy() 让 CustomWidget 接受并保持焦点

QWidget, keyPressEvent

QWidget, setFocusPolicy

工作解决方案:

重要提示:在 GroupBox 的 keyPressEvent() 方法结束时,我们必须将 Event 传递给 super。或者事件不会传播到父窗口小部件:super(QtGui.QGroupBox, self).keyPressEvent(event)

import sys
from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent):
        QtGui.QMainWindow.__init__(self, parent=parent)
        self.setFocusPolicy(QtCore.Qt.StrongFocus) 

    def keyPressEvent(self, event):
        if event.modifiers() == QtCore.Qt.ControlModifier:
            if event.key() == QtCore.Qt.Key_T:
                print 'MainWindow: Control + t'
            if event.key() == QtCore.Qt.Key_M:
                print 'MainWindow: Control + m'

class GroupBox(QtGui.QGroupBox):
    def __init__(self, parent=None):
        QtGui.QGroupBox.__init__(self, parent=parent)
        self.setFocusPolicy(QtCore.Qt.StrongFocus) 

    def keyPressEvent(self, event):
        if event.modifiers() == QtCore.Qt.ControlModifier:
            if event.key() == QtCore.Qt.Key_T:
                print 'GroupBox: Control + t'
            if event.key() == QtCore.Qt.Key_S:
                print 'GroupBox: Control + s'
        super(QtGui.QGroupBox, self).keyPressEvent(event)

class App(MainWindow):
    def __init__(self, parent=None):
        MainWindow.__init__(self, parent=parent) 
        centralWidget = QtGui.QWidget(self) 

        self.setCentralWidget(centralWidget)

        mainLayout=QtGui.QVBoxLayout()
        centralWidget.setLayout(mainLayout)

        groupBox = GroupBox(self)
        mainLayout.addWidget(groupBox)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = App()
    w.show()
    sys.exit(app.exec_())