PyQt小部件键盘焦点 [英] PyQt widget keyboard focus

查看:306
本文介绍了PyQt小部件键盘焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,谢谢您!我大约一个月前开始研究PyQt.那个时候,我遇到了很多问题,几乎总是在这里找到答案.

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.

直到现在.

我对此有一种解决方法,但我认为这是一个杂乱无章的方法,也许有一种适当的方法.我想更好地了解发生了什么.

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.

代码如下:

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_())

按原样,所有键盘输入均由重载的keyPressEvent()函数检测,箭头键除外.我发现有足够多的帖子在谈论这一点,从而感觉到这是因为子窗口小部件(self.view)正在接收它们.我猜想孩子小部件实际上是在接收 all 的击键,但是忽略了正在通过的击键,并吸收了箭头键,这就是为什么它们没有到达父级的keyPressEvent的原因. () 功能.好像是这样,因为如果我取消注释中间的行,则:

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

它的行为符合我的预期-父级的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)

当我添加它时,keyPressEvent()根本看不到任何任何击键.

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

我想我也可以为子级重载keyPressEvent(),然后将所有内容显式传递给父级.但这似乎并不比我的kluge好.

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 ...

推荐答案

默认情况下,QWidget不接受键盘焦点,因此您需要显式启用它:

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)

这篇关于PyQt小部件键盘焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆