Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt6 sometimes uses Enum for integers - why?
Forum Updated to NodeBB v4.3 + New Features

PyQt6 sometimes uses Enum for integers - why?

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 4 Posters 76 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • digitaltrailsD Offline
    digitaltrailsD Offline
    digitaltrails
    wrote last edited by
    #1

    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.

    https://github.com/digitaltrails

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • P Offline
        P Offline
        papajoke
        wrote last edited by papajoke
        #3

        use signal checkStateChanged with qt6 , stateChanged is deprecated

        1 Reply Last reply
        0
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote last edited by
          #4

          Changes in enums types has come up several times on the PyQt mailing list. You might find a better answer there, or a fix if the behavior is unintentional.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved