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.

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

解决方案

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值