QEvent.MouseButtonPress enum type missing in PyQt6?
Asked Answered
P

2

9

In PyQt5, we can validate an event occurrence using QEvent class, for example QEvent.MouseButtonPress. In PyQt6 the statement is no longer valid. I have checked the members of both PyQt6.QtCore.QEvent and PyQt6.QtGui.QMouseEvent classes, I don't seem to be able to locate the correct Enum class containing the MouseButtonPress event value.

PyQt5 Example I am trying to translate to 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...')

Updated: If I print the members of both QEvent and QMouseEvent, this is all the members are available.

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']
Preconcert answered 17/2, 2021 at 4:12 Comment(2)
If I print the members of QEvent, this is all are available. 'accept', 'clone', 'ignore', 'isAccepted', 'isInputEvent', 'isPointerEvent', 'isSinglePointEvent' PS: Added additional detail in the post.Preconcert
Oh. Can't believe I miss that. Thank you so much.Preconcert
K
11

One of the main changes that PyQt6 enums use python enums so you must use the enumeration name as an intermediary, in your case MouseButtonPress belongs to the Type enum and RightButton to MouseButtons so you must change it to:

def eventFilter(self, QObject, event):
    if event.type() == QEvent.Type.MouseButtonPress:
        if event.button() == Qt.MouseButtons.RightButton:
            print("Right button clicked")

    return True
Kra answered 17/2, 2021 at 4:43 Comment(0)
E
1
import sys

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QWidget


class Widget(QWidget):

    def __init__(self):
        super().__init__()

    def mousePressEvent(self, event):
        if event.button() == Qt.MouseButton.LeftButton:
            print("left")
            print(event.pos().x(), event.pos().y())
        elif event.button() == Qt.MouseButton.RightButton:
            print("right")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec())
Epistrophe answered 28/1, 2023 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.