PySide6 QCheckBox stateChanged() generates state event int instead of Qt.CheckState
-
wrote on 8 Jan 2023, 20:30 last edited by Erriez 1 Aug 2023, 21:02
PySide6 6.4.1 PIP on Ubuntu and Windows contains a
QCheckBox
withstateChanged()
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 argumentstate
is typeint
instead 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.PartiallyChecked
orQt.Unchecked
to 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:
-
wrote on 9 Jan 2023, 09:51 last edited by
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
QCheckBox
withstateChanged()
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 argumentstate
is typeint
instead 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.PartiallyChecked
orQt.Unchecked
to 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?
wrote on 9 Jan 2023, 14:51 last edited by JonB 1 Sept 2023, 15:13@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
- 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.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
wrote on 9 Jan 2023, 19:02 last edited by Erriez 1 Sept 2023, 19:06 -
wrote on 9 Jan 2023, 19:42 last edited by
@Erriez
That example hasfrom 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...?
-
wrote on 10 Jan 2023, 07:34 last edited by
Our examples gallery: https://doc.qt.io/qtforpython-6/examples/index.html
-
wrote on 10 Jan 2023, 20:37 last edited by
Thanks for pointing to the (advanced) examples. That's the right way to go.
1/7