[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    QCheckBox.stateChanged()
From:       Aron Bierbaum <aronbierbaum () gmail ! com>
Date:       2022-10-12 13:07:17
Message-ID: CALOK6rsTDNxrzUtjzLFAW2qK8gdRg-vD17vY3ng2=WEz-QSTUQ () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


The QCheckBox.stateChanged() signal [1] is documented to include the state
as an integer. Since QtCore.Qt.CheckState is now an enum.Enum it can not be
compared against the integer value received with the signal. I like the
type safety that comes with using enum.Enum and the stateChanged() signal
is documented to take an integer, but it makes handling the signal more
difficult than it should be.

There seem to be a few different ways that developers can work around this.

 - Convert the integer QtCore.Qt.CheckState(newState)
 - Compare the integer against the enum's value property
QtCore.Qt.CheckState.Checked.value

Is there a better way to handle this? I have attached an example
that should run with PyQt5 and PyQt6.

Regards,
Aron


[1] https://doc.qt.io/qt-6/qcheckbox.html#stateChanged

[Attachment #5 (text/html)]

<div dir="ltr">The QCheckBox.stateChanged() signal [1] is documented to include the \
state as an integer. Since QtCore.Qt.CheckState is now an enum.Enum it can not be \
compared against the integer value received with the signal. I like the type safety \
that comes with using enum.Enum and the stateChanged() signal is documented to take \
an integer, but it makes handling the signal more difficult than it should \
be.<div><br></div><div>There seem to be a few different ways that developers can work \
around this.</div><div><br></div><div>  - Convert the integer  \
QtCore.Qt.CheckState(newState)</div><div>  - Compare the integer against the \
enum&#39;s value property \
QtCore.Qt.CheckState.Checked.value</div><div><br></div><div>Is there a better way to \
handle this? I have attached an example that  should run with PyQt5 and \
PyQt6.</div><div><br></div><div>Regards,</div><div>Aron</div><div><br><div><br></div><div>[1] \
<a href="https://doc.qt.io/qt-6/qcheckbox.html#stateChanged">https://doc.qt.io/qt-6/qcheckbox.html#stateChanged</a><br></div></div></div>



["test_check.py" (text/plain)]

try:
   from PyQt6 import QtCore, QtGui, QtWidgets
except ImportError:
   from PyQt5 import QtCore, QtGui, QtWidgets


def main():
   app = QtWidgets.QApplication([])



   w = QtWidgets.QWidget()
   layout = QtWidgets.QVBoxLayout()
   w.setLayout(layout)
   check = QtWidgets.QCheckBox("Does this work?")
   layout.addWidget(check)
   label1 = QtWidgets.QLabel("")
   layout.addWidget(label1)
   label2 = QtWidgets.QLabel("")
   layout.addWidget(label2)
   label3 = QtWidgets.QLabel("")
   layout.addWidget(label3)

   def onCheckStateChanged(newState):
      label1.setText("'%s' '%s'" % (type(newState), newState))
      state = check.checkState()
      label2.setText("'%s' '%s'" % (type(state), state))
      label3.setText("equal: %s" % (newState == state))

   check.stateChanged.connect(onCheckStateChanged)

   w.show()

   app.exec()


if __name__ == "__main__":
   main()


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic