diff --git a/src/widgets/widgets/qcheckbox.cpp b/src/widgets/widgets/qcheckbox.cpp index 151c8b1e94..cc013c8c7e 100644 --- a/src/widgets/widgets/qcheckbox.cpp +++ b/src/widgets/widgets/qcheckbox.cpp @@ -28,7 +28,7 @@ public: uint tristate : 1; uint noChange : 1; uint hovering : 1; - uint publishedState : 2; + Qt::CheckState publishedState : 2; void init(); }; @@ -89,6 +89,7 @@ public: \sa QAbstractButton, QRadioButton */ +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) /*! \fn void QCheckBox::stateChanged(int state) @@ -97,6 +98,16 @@ public: \a state contains the checkbox's new Qt::CheckState. */ +#else +/*! + \fn void QCheckBox::stateChanged(Qt::CheckState state) + + This signal is emitted whenever the checkbox's state changes, i.e., + whenever the user checks or unchecks it. + + \a state contains the checkbox's new Qt::CheckState. +*/ +#endif /*! \property QCheckBox::tristate @@ -224,7 +235,7 @@ void QCheckBox::setCheckState(Qt::CheckState state) setChecked(state != Qt::Unchecked); d->blockRefresh = false; d->refresh(); - if ((uint)state != d->publishedState) { + if (state != d->publishedState) { d->publishedState = state; emit stateChanged(state); } @@ -319,7 +330,7 @@ void QCheckBox::checkStateSet() Q_D(QCheckBox); d->noChange = false; Qt::CheckState state = checkState(); - if ((uint)state != d->publishedState) { + if (state != d->publishedState) { d->publishedState = state; emit stateChanged(state); } diff --git a/src/widgets/widgets/qcheckbox.h b/src/widgets/widgets/qcheckbox.h index ab240b5500..e385dadbb8 100644 --- a/src/widgets/widgets/qcheckbox.h +++ b/src/widgets/widgets/qcheckbox.h @@ -36,7 +36,11 @@ public: void setCheckState(Qt::CheckState state); Q_SIGNALS: +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) void stateChanged(int); +#else + void stateChanged(Qt::CheckState); +#endif protected: bool event(QEvent *e) override; diff --git a/tests/auto/widgets/widgets/qcheckbox/tst_qcheckbox.cpp b/tests/auto/widgets/widgets/qcheckbox/tst_qcheckbox.cpp index 7a54a6e5e6..9055b13901 100644 --- a/tests/auto/widgets/widgets/qcheckbox/tst_qcheckbox.cpp +++ b/tests/auto/widgets/widgets/qcheckbox/tst_qcheckbox.cpp @@ -191,24 +191,29 @@ void tst_QCheckBox::toggled() void tst_QCheckBox::stateChanged() { QCheckBox testWidget; - int cur_state = -1; + QCOMPARE(testWidget.checkState(), Qt::Unchecked); + + Qt::CheckState cur_state = Qt::Unchecked; QSignalSpy stateChangedSpy(&testWidget, &QCheckBox::stateChanged); - connect(&testWidget, &QCheckBox::stateChanged, this, [&](int state) { ++cur_state = state; }); + connect(&testWidget, &QCheckBox::stateChanged, this, [&](auto state) { cur_state = Qt::CheckState(state); }); testWidget.setChecked(true); - QCoreApplication::processEvents(); - QCOMPARE(cur_state, 2); + QVERIFY(QTest::qWaitFor([&]() { return stateChangedSpy.size() == 1; })); + QCOMPARE(stateChangedSpy.size(), 1); + QCOMPARE(cur_state, Qt::Checked); + QCOMPARE(testWidget.checkState(), Qt::Checked); - cur_state = -1; testWidget.setChecked(false); - QCoreApplication::processEvents(); - QCOMPARE(cur_state, 0); + QVERIFY(QTest::qWaitFor([&]() { return stateChangedSpy.size() == 2; })); + QCOMPARE(stateChangedSpy.size(), 2); + QCOMPARE(cur_state, Qt::Unchecked); + QCOMPARE(testWidget.checkState(), Qt::Unchecked); - cur_state = -1; testWidget.setCheckState(Qt::PartiallyChecked); - QCoreApplication::processEvents(); - QCOMPARE(cur_state, 1); - + QVERIFY(QTest::qWaitFor([&]() { return stateChangedSpy.size() == 3; })); QCOMPARE(stateChangedSpy.size(), 3); + QCOMPARE(cur_state, Qt::PartiallyChecked); + QCOMPARE(testWidget.checkState(), Qt::PartiallyChecked); + testWidget.setCheckState(Qt::PartiallyChecked); QCoreApplication::processEvents(); QCOMPARE(stateChangedSpy.size(), 3);