将keyPressEvent添加到PyQT

时间:2015-06-27 20:41:26

标签: qt pyqt4

我有一个现有的PyQT代码:

class MainWindow(QMainWindow, WindowMixin):

    def __init__(self, filename=None):
        super(MainWindow, self).__init__()
        self.setWindowTitle(__appname__)
    ...
    def resizeEvent(self, event):
    def queueEvent(self, event):

我添加了keyPressEvent:

def keyPressEvent(self, event):
    print "pressed"

事实证明,当我在QMainWindow中的某个小部件中输入文本时,它只接收事件。当图片具有焦点时,它不会生成按键事件。我该如何解决这个问题?

enter image description here

1 个答案:

答案 0 :(得分:0)

也许您的子窗口小部件(图片窗口小部件)也在等待keyPressEvent并且不会将它传播给父设备?

class ParentWidget(...):
    def keyPressEvent(self, event):
        print "pressed"

class ChildWidget(...):
    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_<Some_key>:
            ...
        # Even if the pressed key is not <Some_key>, ParentWidget won't receive this event; 
        # for sending an event to the parent you must reject it:
        else:
            event.ignore()