Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-104688

State variable in QCheckBox::stateChanged should be emitted as Qt::CheckState instead of int

XMLWordPrintable

    • 2
    • 37b47ebf9 (dev), 5a96d13bb (dev), 90822833f (dev), 3b3b6f1ad (6.6)
    • Foundation Sprint 88, Foundation Sprint 89, Foundation Sprint 90

      The Qt::stateChanged signal is currently emitted with a value of type integer, even though the signal documentation says that the state parameter "contains the checkbox's new Qt::CheckState." It seems like this signal should be emitted with a value of type Qt::CheckState instead. The two spots where this signal is emitted in qcheckbox.cpp, it's emitted with a Qt::CheckState value that's implicitly converted to an integer. For comparison, the QScroller::stateChanged signal, which is similar to QCheckBox::stateChanged, is emitted with a QScroller::State value rather than an integer.

      The integer parameter of QCheckBox::stateChanged is particularly problematic when working with PyQt 6, because PyQt 6 enforces type checking for enums, which means that integers will never compare equal to any Qt::CheckState value. In other words, if the following slot is connected to QCheckBox::stateChanged in PyQt 6

      def onStateChanged(state):
          if state == Qt.CheckState.Checked:
              print("Checked")
          elif state == Qt.CheckState.Unchecked:
              print("Unchecked")
          elif state == Qt.CheckState.PartiallyChecked:
              print("Partially checked")
          else:
              print("Unrecognized value")
      

      then it will always print "Unrecognized value" because it will be called with 0, 1, or 2, instead of Checked, Unchecked, or PartiallyChecked. To work around this, comparisons need to be done against, e.g. Qt.CheckState.Checked.value (the ".value" at the end will convert the enum into an integer) instead.

      Q_SIGNALS:
          void stateChanged(int);
      

        For Gerrit Dashboard: QTBUG-104688
        # Subject Branch Project Status CR V

            mmutz Marc Mutz
            kevkeating Kevin Keating
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            1 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes