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

    Details

      Description

      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);
      

        Attachments

        1. qtbug104688.py
          1 kB
          Friedemann Kleint
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            qt.team.quick.subscriptions Qt Quick and Widgets Team
            Reporter:
            kevkeating Kevin Keating
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes