PyQt widget keyboard focus

删除回忆录丶 提交于 2019-12-25 14:30:33

问题


First off -- thanks for this group! I started delving into PyQt a month or so ago. In that time, I've bumped up against many questions, and virtually always found an answer here.

Until now.

I have a workaround for this, but I think it's a kluge and there probably is a proper way. I'd like to understand better what's going on.

Here's the code:

from PyQt5.QtCore       import *
from PyQt5.QtGui        import *
from PyQt5.QtWidgets    import *


class FormWidget(QWidget):
    def __init__(self, parent):        
        super(FormWidget, self).__init__(parent)

        # Create view with image in it
        self.image = QGraphicsPixmapItem(QPixmap())
        self.scene = QGraphicsScene()
        self.scene.addItem(self.image)
        self.view = QGraphicsView(self.scene)

        self.hlayout = QHBoxLayout()
        self.hlayout.addWidget(self.view)
        self.setLayout(self.hlayout)

#       self.view.keyPressEvent = self.keyPressEvent

    def keyPressEvent(self, event):
        key = event.key()
        mod = int(event.modifiers())
        print(
            "<{}> Key 0x{:x}/{}/ {} {} {}".format(
                self,
                key,
                event.text(),
                "  [+shift]" if event.modifiers() & Qt.SHIFT else "",
                "  [+ctrl]" if event.modifiers() & Qt.CTRL else "",
                "  [+alt]" if event.modifiers() & Qt.ALT else ""
            )
        )


class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        form = FormWidget(self) 
        self.setCentralWidget(form) 


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

As is, all keyboard input is detected by the overloaded keyPressEvent() function, except arrow keys. I've found enough posts talking about this to have a sense that it is because the child widget (self.view) is receiving them. I presume the child widget is, in fact, receiving all the keystrokes, but ignoring the ones that are getting through, and sucking up the arrow keys, which is why they aren't getting to the parent's keyPressEvent() function. That seems to be so, because if I uncomment the line in the middle:

        self.view.keyPressEvent = self.keyPressEvent

It behaves as I expect -- the parent's keyPressEvent() gets all the keystrokes, arrows included.

How would I tell the child widget to ignore all keystrokes? I thought maybe this:

        self.view.setFocusPolicy(Qt.NoFocus)

When I add that, keyPressEvent() doesn't see any keystrokes at all.

I suppose I could overload keyPressEvent() for the child as well, and just explicitly pass everything up to the parent. But that doesn't seem better than my kluge.

I think I must be misunderstanding something here.

Thanks. Just looking to learn ...


回答1:


Rather than subclassing the child widget or attempting to prevent keystrokes from reaching it, you should consider using an eventFilter to capture events on the child widget. You will see all events before the child widget, and you can suppress or transform them.

http://doc.qt.io/qt-5.5/qobject.html#eventFilter




回答2:


By default, a QWidget does not accept the keyboard focus, so you need to enable it explicitly:

class FormWidget(QWidget):
    def __init__(self, parent):
        ... 
        self.setFocusPolicy(Qt.StrongFocus)


来源:https://stackoverflow.com/questions/35046381/pyqt-widget-keyboard-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!