请考虑以下代码。我在BeagleBone上运行它(libqt5widgets5:armhf 5.7.1 + dfsg-3 + b1)。它会触发FocusInEvent
事件。当我在行(***)中用QWidget替换QDialog时,它会停止工作。为什么呢?
对于不同的PyQt5安装,这是不可重现的(我使用Cygwin进行检查)。
[1]这可能与:focus don't work in PyQt (QT 5.7)有关,也可能与之无关 [2]在C ++中同样的事情也不起作用,https://github.com/leshikus/qt-test
#!/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_())