如何检查 PyQt6 中的 MouseButtonPress 事件?
How to check MouseButtonPress event in PyQt6?
在 PyQt5 中,我们可以使用 QEvent class 来验证事件的发生,例如 QEvent.MouseButtonPress。在 PyQt6 中,该语句不再有效。我检查了 PyQt6.QtCore.QEvent
和 PyQt6.QtGui.QMouseEvent
class 的成员,我似乎无法找到包含 MouseButtonPress 事件值的正确枚举 class。
PyQt5 示例我正在尝试转换为 PyQt6
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QEvent, Qt
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.resize(800, 400)
self.installEventFilter(self)
def eventFilter(self, QObject, event):
if event.type() == QEvent.MouseButtonPress: # <-- No longer work in PyQt6
if event.button() == Qt.RightButton: # <-- Becomes event.button() == Qt.MouseButtons.RightButton
print('Right button clicked')
return True
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = AppDemo()
demo.show()
try:
sys.exit(app.exec_())
except SystemExit:
print('Closing Window...')
更新:
如果我打印 QEvent 和 QMouseEvent 的成员,这就是所有可用的成员。
print('Members of PyQt6.QtCore.QEvent')
print(dir(QEvent))
print('-'*50)
print('Members of PyQt6.QtCore.QMouseEvent')
print(dir(QMouseEvent))
>>>
Members of PyQt6.QtCore.QEvent
['Type', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'accept', 'clone', 'ignore', 'isAccepted', 'isInputEvent', 'isPointerEvent', 'isSinglePointEvent', 'registerEventType', 'setAccepted', 'spontaneous', 'type']
--------------------------------------------------
Members of PyQt6.QtCore.QMouseEvent
['Type', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'accept', 'allPointsAccepted', 'button', 'buttons', 'clone', 'device', 'deviceType', 'exclusivePointGrabber', 'globalPosition', 'ignore', 'isAccepted', 'isBeginEvent', 'isEndEvent', 'isInputEvent', 'isPointerEvent', 'isSinglePointEvent', 'isUpdateEvent', 'modifiers', 'point', 'pointById', 'pointCount', 'pointerType', 'pointingDevice', 'points', 'position', 'registerEventType', 'scenePosition', 'setAccepted', 'setExclusivePointGrabber', 'spontaneous', 'timestamp', 'type']
PyQt6 枚举使用 python 枚举的主要变化之一,因此您必须使用枚举名称作为中介,在您的情况下,MouseButtonPress 属于 Type 枚举,RightButton 属于 MouseButtons,因此您必须将其更改为:
def eventFilter(self, QObject, event):
if event.type() == QEvent.<b>Type</b>.MouseButtonPress:
if event.button() == Qt.<b>MouseButtons</b>.RightButton:
print("Right button clicked")
return True
在 PyQt5 中,我们可以使用 QEvent class 来验证事件的发生,例如 QEvent.MouseButtonPress。在 PyQt6 中,该语句不再有效。我检查了 PyQt6.QtCore.QEvent
和 PyQt6.QtGui.QMouseEvent
class 的成员,我似乎无法找到包含 MouseButtonPress 事件值的正确枚举 class。
PyQt5 示例我正在尝试转换为 PyQt6
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QEvent, Qt
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.resize(800, 400)
self.installEventFilter(self)
def eventFilter(self, QObject, event):
if event.type() == QEvent.MouseButtonPress: # <-- No longer work in PyQt6
if event.button() == Qt.RightButton: # <-- Becomes event.button() == Qt.MouseButtons.RightButton
print('Right button clicked')
return True
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = AppDemo()
demo.show()
try:
sys.exit(app.exec_())
except SystemExit:
print('Closing Window...')
更新: 如果我打印 QEvent 和 QMouseEvent 的成员,这就是所有可用的成员。
print('Members of PyQt6.QtCore.QEvent')
print(dir(QEvent))
print('-'*50)
print('Members of PyQt6.QtCore.QMouseEvent')
print(dir(QMouseEvent))
>>>
Members of PyQt6.QtCore.QEvent
['Type', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'accept', 'clone', 'ignore', 'isAccepted', 'isInputEvent', 'isPointerEvent', 'isSinglePointEvent', 'registerEventType', 'setAccepted', 'spontaneous', 'type']
--------------------------------------------------
Members of PyQt6.QtCore.QMouseEvent
['Type', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'accept', 'allPointsAccepted', 'button', 'buttons', 'clone', 'device', 'deviceType', 'exclusivePointGrabber', 'globalPosition', 'ignore', 'isAccepted', 'isBeginEvent', 'isEndEvent', 'isInputEvent', 'isPointerEvent', 'isSinglePointEvent', 'isUpdateEvent', 'modifiers', 'point', 'pointById', 'pointCount', 'pointerType', 'pointingDevice', 'points', 'position', 'registerEventType', 'scenePosition', 'setAccepted', 'setExclusivePointGrabber', 'spontaneous', 'timestamp', 'type']
PyQt6 枚举使用 python 枚举的主要变化之一,因此您必须使用枚举名称作为中介,在您的情况下,MouseButtonPress 属于 Type 枚举,RightButton 属于 MouseButtons,因此您必须将其更改为:
def eventFilter(self, QObject, event):
if event.type() == QEvent.<b>Type</b>.MouseButtonPress:
if event.button() == Qt.<b>MouseButtons</b>.RightButton:
print("Right button clicked")
return True