Qt QWidget没有焦点事件
考虑以下代码。我在BeagleBone上运行它(libqt5widgets5:armhf5.7.1+dfsg-3+b1)。它会触发Qt QWidget没有焦点事件,qt,pyqt5,Qt,Pyqt5,考虑以下代码。我在BeagleBone上运行它(libqt5widgets5:armhf5.7.1+dfsg-3+b1)。它会触发FocusInEvent事件。当我用行(***)中的QWidget替换QDialog时,它停止工作。为什么? 对于不同的PyQt5安装,这是不可复制的(我使用Cygwin进行检查) [1] 这可能与, (2)C++中同样的事情也不起作用, 您需要通过调用setFocusPolicy(Qt::FocusPolicy策略) 有关焦点策略的更多信息,请查看 Python特定
FocusInEvent
事件。当我用行(***)中的QWidget替换QDialog时,它停止工作。为什么?
对于不同的PyQt5安装,这是不可复制的(我使用Cygwin进行检查)
[1] 这可能与,
(2)C++中同样的事情也不起作用,
您需要通过调用
setFocusPolicy(Qt::FocusPolicy策略)
有关焦点策略的更多信息,请查看
Python特定文档可用于添加
w.setFocusPolicy(Qt.StrongFocus)
也无法尝试在MyWidget的初始值设定项中调用self.setFocusPolicy(Qt.StrongFocus)
。运气也不好。请注意,该缺陷在不同的平台上是不可复制的。如果您认为您发现了一个缺陷/错误功能,则需要。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QSlider, QCheckBox, QVBoxLayout, QMainWindow, QDialog
from PyQt5.QtCore import Qt, QEvent
class MyWidget(QSlider):
def __init__(self):
super().__init__()
def focusInEvent(self, event):
super().focusInEvent(event)
print('focusInEvent')
def focusOutEvent(self, event):
super().focusOutEvent(event)
print('focusOutEvent')
def eventFilter(self, source, event):
print("eventFilter", event.type(), source)
return super().eventFilter(source, event)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QDialog() # got replaced with QWidget (***)
w.resize(300, 200)
w.move(30, 30)
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
s = MyWidget()
layout.addWidget(s)
w.setLayout(layout)
s.installEventFilter(s)
s.setFocus()
w.show()
sys.exit(app.exec_())