PySide6 QCheckBox stateChanged() generates state event int instead of Qt.CheckState
-
PySide6 6.4.1 PIP on Ubuntu and Windows contains a
QCheckBoxwithstateChanged()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
QCheckBoxgenerated event argumentstateis typeintinstead of enumCheckState: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.PartiallyCheckedorQt.Uncheckedto anint.
Casting withif 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?
- QCheckBox documentation:
-
Qt.Checked.value can be used to obtain the int value of the Enum-derived class.
-
PySide6 6.4.1 PIP on Ubuntu and Windows contains a
QCheckBoxwithstateChanged()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
QCheckBoxgenerated event argumentstateis typeintinstead of enumCheckState: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.PartiallyCheckedorQt.Uncheckedto anint.
Casting withif 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?
@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.Uncheckedetc. 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 - QCheckBox documentation:
-
@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.Uncheckedetc. 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 -
-
Our examples gallery: https://doc.qt.io/qtforpython-6/examples/index.html