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 Update on Monday, May 27th 2025

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.
  • E Offline
    E Offline
    Erriez
    wrote on 8 Jan 2023, 20:30 last edited by Erriez 1 Aug 2023, 21:02
    #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?

    J 1 Reply Last reply 9 Jan 2023, 14:51
    0
    • F Offline
      F Offline
      friedemannkleint
      wrote on 9 Jan 2023, 09:51 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
      • E Erriez
        8 Jan 2023, 20:30

        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?

        J Offline
        J Offline
        JonB
        wrote on 9 Jan 2023, 14:51 last edited by JonB 1 Sept 2023, 15:13
        #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

        E 1 Reply Last reply 9 Jan 2023, 19:02
        1
        • J JonB
          9 Jan 2023, 14:51

          @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

          E Offline
          E Offline
          Erriez
          wrote on 9 Jan 2023, 19:02 last edited by Erriez 1 Sept 2023, 19:06
          #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.

          J 1 Reply Last reply 9 Jan 2023, 19:42
          0
          • E Erriez
            9 Jan 2023, 19:02

            @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.

            J Offline
            J Offline
            JonB
            wrote on 9 Jan 2023, 19:42 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 10 Jan 2023, 07:34 last edited by
              #6

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

              1 Reply Last reply
              1
              • E Offline
                E Offline
                Erriez
                wrote on 10 Jan 2023, 20:37 last edited by
                #7

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

                1 Reply Last reply
                0

                1/7

                8 Jan 2023, 20:30

                • Login

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