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. PySide6 QCheckBox stateChanged() generates state event int instead of Qt.CheckState
Forum Updated to NodeBB v4.3 + New Features

PySide6 QCheckBox stateChanged() generates state event int instead of Qt.CheckState

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 3 Posters 2.4k Views
  • 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.
  • ErriezE Offline
    ErriezE Offline
    Erriez
    wrote on last edited by Erriez
    #1

    PySide6 6.4.1 PIP on Ubuntu and Windows contains a QCheckBox with stateChanged() API call to generate an event when the checkbox state changes.

    • QCheckBox documentation:
      • state contains the checkbox’s new CheckState.
    • PySide6.QtCore.Qt.CheckState documentation:
      • This enum describes the state...
      • Qt.Checked: The item is checked..

    The QCheckBox generated event argument state is type int instead of enum CheckState:

    Testcase below demonstrates not working checkbox event state:

    from PySide6.QtWidgets import QApplication, QWidget, QCheckBox
    from PySide6.QtCore import Qt
    import sys
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
    
            self.setGeometry(300, 300, 250, 150)
            self.setWindowTitle('QCheckBox')
    
            checkbox = QCheckBox('Show title', self)
            checkbox.move(20, 20)
            checkbox.toggle()
            checkbox.setTristate(True)
            checkbox.stateChanged.connect(self.onCheckboxChange)
    
        def onCheckboxChange(self, state):
            # state = int and not Qt.Checked or Qt.Unchecked
            # This is not matching with the documentation
    
            print('Qt.Checked type: {}'.format(type(Qt.Checked)))
            print('Event: {}, type: {}'.format(state, type(state)))
    
            # Always setting title to UNKNOWN as this is an incorrect type comparison
            if state == Qt.Unchecked:
                self.setWindowTitle('Unchecked')
            elif state == Qt.PartiallyChecked:
                self.setWindowTitle('PartiallyChecked')
            elif state == Qt.Checked:
                self.setWindowTitle('Checked')
            else:
                self.setWindowTitle('UNKNOWN')
    
    def main():
        app = QApplication(sys.argv)
        window = Window()
        window.show()
        sys.exit(app.exec())
    
    if __name__ == '__main__':
        main()
    

    Output:

    # When unchecking checkbox:
    Qt.Checked type: <enum 'CheckState'>
    Event: 0, type: <class 'int'>    <= Expected Qt.Unchecked:
    
    # When partially checking checkbox:
    Qt.Checked type: <enum 'CheckState'>
    Event: 1, type: <class 'int'>    <= Expected Qt.PartiallyChecked:
    
    # When checking checkbox:
    Qt.Checked type: <enum 'CheckState'>
    Event: 2, type: <class 'int'>    <= Expected Qt.Checked:
    

    I don't know how to convert the Qt.Checked , Qt.PartiallyChecked or Qt.Unchecked to an int.
    Casting with if state == int(Qt.Checked) generates:

    • TypeError: int() argument must be a string, a bytes-like object or a real number, not 'CheckState'

    Is this expected behavior?

    JonBJ 1 Reply Last reply
    0
    • F Offline
      F Offline
      friedemannkleint
      wrote on last edited by
      #2

      Qt.Checked.value can be used to obtain the int value of the Enum-derived class.

      1 Reply Last reply
      1
      • ErriezE Erriez

        PySide6 6.4.1 PIP on Ubuntu and Windows contains a QCheckBox with stateChanged() API call to generate an event when the checkbox state changes.

        • QCheckBox documentation:
          • state contains the checkbox’s new CheckState.
        • PySide6.QtCore.Qt.CheckState documentation:
          • This enum describes the state...
          • Qt.Checked: The item is checked..

        The QCheckBox generated event argument state is type int instead of enum CheckState:

        Testcase below demonstrates not working checkbox event state:

        from PySide6.QtWidgets import QApplication, QWidget, QCheckBox
        from PySide6.QtCore import Qt
        import sys
        
        class Window(QWidget):
            def __init__(self):
                super().__init__()
        
                self.setGeometry(300, 300, 250, 150)
                self.setWindowTitle('QCheckBox')
        
                checkbox = QCheckBox('Show title', self)
                checkbox.move(20, 20)
                checkbox.toggle()
                checkbox.setTristate(True)
                checkbox.stateChanged.connect(self.onCheckboxChange)
        
            def onCheckboxChange(self, state):
                # state = int and not Qt.Checked or Qt.Unchecked
                # This is not matching with the documentation
        
                print('Qt.Checked type: {}'.format(type(Qt.Checked)))
                print('Event: {}, type: {}'.format(state, type(state)))
        
                # Always setting title to UNKNOWN as this is an incorrect type comparison
                if state == Qt.Unchecked:
                    self.setWindowTitle('Unchecked')
                elif state == Qt.PartiallyChecked:
                    self.setWindowTitle('PartiallyChecked')
                elif state == Qt.Checked:
                    self.setWindowTitle('Checked')
                else:
                    self.setWindowTitle('UNKNOWN')
        
        def main():
            app = QApplication(sys.argv)
            window = Window()
            window.show()
            sys.exit(app.exec())
        
        if __name__ == '__main__':
            main()
        

        Output:

        # When unchecking checkbox:
        Qt.Checked type: <enum 'CheckState'>
        Event: 0, type: <class 'int'>    <= Expected Qt.Unchecked:
        
        # When partially checking checkbox:
        Qt.Checked type: <enum 'CheckState'>
        Event: 1, type: <class 'int'>    <= Expected Qt.PartiallyChecked:
        
        # When checking checkbox:
        Qt.Checked type: <enum 'CheckState'>
        Event: 2, type: <class 'int'>    <= Expected Qt.Checked:
        

        I don't know how to convert the Qt.Checked , Qt.PartiallyChecked or Qt.Unchecked to an int.
        Casting with if state == int(Qt.Checked) generates:

        • TypeError: int() argument must be a string, a bytes-like object or a real number, not 'CheckState'

        Is this expected behavior?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Erriez said in PySide6 QCheckBox stateChanged() generates state event int instead of Qt.CheckState:

        Casting with if state == int(Qt.Checked) generates:
        Is this expected behavior?

        Apparently so! Have a read of https://stackoverflow.com/questions/72161415/qt-checkstate-checked-2-and-qt-checkstate-checked-0. You are supposed to do the "cast" the other way round, not from enum to int but from int to the enum:

        if Qt.CheckState(state) == Qt.CheckState.Unchecked
        

        etc. Or you can make your if ... elif ... code neater with:

        state = Qt.CheckState(state)
        if state == Qt.CheckState.Unchecked;
        elif state == Qt.CheckState.PartiallyChecked:
        elif state == Qt.CheckState.Checked:
        

        You can indeed also use @friedemannkleint's state == Qt.CheckState.Unchecked.value

        ErriezE 1 Reply Last reply
        1
        • JonBJ JonB

          @Erriez said in PySide6 QCheckBox stateChanged() generates state event int instead of Qt.CheckState:

          Casting with if state == int(Qt.Checked) generates:
          Is this expected behavior?

          Apparently so! Have a read of https://stackoverflow.com/questions/72161415/qt-checkstate-checked-2-and-qt-checkstate-checked-0. You are supposed to do the "cast" the other way round, not from enum to int but from int to the enum:

          if Qt.CheckState(state) == Qt.CheckState.Unchecked
          

          etc. Or you can make your if ... elif ... code neater with:

          state = Qt.CheckState(state)
          if state == Qt.CheckState.Unchecked;
          elif state == Qt.CheckState.PartiallyChecked:
          elif state == Qt.CheckState.Checked:
          

          You can indeed also use @friedemannkleint's state == Qt.CheckState.Unchecked.value

          ErriezE Offline
          ErriezE Offline
          Erriez
          wrote on last edited by Erriez
          #4

          @JonB Thanks for your explanation. I confirm that both state = Qt.CheckState(state) and state == Qt.CheckState.Unchecked.value works which answered my question.

          And the reason is that we as beginner start with an incorrect example.

          JonBJ 1 Reply Last reply
          0
          • ErriezE Erriez

            @JonB Thanks for your explanation. I confirm that both state = Qt.CheckState(state) and state == Qt.CheckState.Unchecked.value works which answered my question.

            And the reason is that we as beginner start with an incorrect example.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Erriez
            That example has

            from PySide import
            

            which looks awfully like PySide 1 to me, from yonks ago! I think the behaviour you are asking about would have first appeared in PySide6, when they "tightened" handling of enumerated types...?

            1 Reply Last reply
            0
            • F Offline
              F Offline
              friedemannkleint
              wrote on last edited by
              #6

              Our examples gallery: https://doc.qt.io/qtforpython-6/examples/index.html

              1 Reply Last reply
              1
              • ErriezE Offline
                ErriezE Offline
                Erriez
                wrote on last edited by
                #7

                Thanks for pointing to the (advanced) examples. That's the right way to go.

                1 Reply Last reply
                0

                • Login

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