PyQt6 sometimes uses Enum for integers - why?
-
I'm converting a script I wrote from PyQt5 to support both PyQt5 and PyQt6 (PyQt6 6.8). In some cases PyQt6 is using Enum for integer values, in other cases it's using IntEnum or IntFlag. The problem I see is that Enum's cannot be compared to int values, which make these Enum cases inconsistent both with other IntEnums/IntFlags in PyQt6 and also when code is run in both PyQt6 and PyQt5. I'm wondering why this is so?
For example. The following works with PyQt5, but the comparison fails with PqQt6
def _set_auto_monitoring(checked: int) -> None: if checked == Qt.CheckState.Checked: # in PyQt6, Qt.CheckState.Checked is an Enum print("auto is on") my_checkbox.stateChanged.connect(_set_auto_monitoring)
The following works with PyQt6, but cause a runtime error with PqQt5
def _set_auto_monitoring(checked: int) -> None: if checked == Qt.CheckState.Checked.value: # in PyQt5 Qt.CheckState.Checked is an int print("auto is on") my_checkbox.stateChanged.connect(_set_auto_monitoring)
In other cases where Qt options are IntEnum and IntFlag, then PyQt5 and PyQt6 behave the same.
BTW, when I find cases such as the above I'm creating code that will work for both by doing:
def intV(type_id: QMetaType.Type|int) -> int: return type_id.value if isinstance(type_id, Enum) else type_id def _set_auto_monitoring(checked: int) -> None: if checked == intV(Qt.CheckState.Checked.value): print("auto is on") my_checkbox.stateChanged.connect(_set_auto_monitoring)
Using QDBusArgument is another similar case, where I also have to do:
QDBusArgument(0, intV(QMetaType.Type.UInt))
Anyway, being new to PyQt6, I thought some here might have some explanations or advice on why things are a bit inconsistent and how best to achieve or test portability between PyQt5 and PyQt6.
Michael.
-
Hi and welcome to devnet,
Do you encounter the same behaviour if you use PySide6 ?
Note that PyQt are bindings from Riverbank Computing so it might partly be related to how the bindings are generated.