QCheckBox: add new checkStateChanged(Qt::CheckState) signal

... to replace the old stateChanged(int) one, which a) had the wrong
name and b) the wrong argument type.

Mark the old one as \obsolete, so new users don't see it
anymore. Prepare for deprecation in the test, but don't actually
deprecate, yet (this author does not know how to deprecate signals).

Found in API-review.

Amends 37b47ebf946ef1a37573107375fbe5fc0eb1e6d2.

As a drive-by, replace explicit qWait() calls with QTRY_COMPARE().

[ChangeLog][QtWidgets][QCheckBox] Added new
checkStateChanged(Qt::CheckState) signal, obsoleting
stateChanged(int).

Fixes: QTBUG-104688
Change-Id: I01791fd003b752c47d99bea65151202be9175c21
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Marc Mutz 2023-08-11 21:22:57 +02:00 committed by Volker Hilsheimer
parent 4367abb75a
commit 5a96d13bb5
3 changed files with 24 additions and 13 deletions

View File

@ -59,7 +59,7 @@ public:
\endtable
Whenever a checkbox is checked or cleared, it emits the signal
stateChanged(). Connect to this signal if you want to trigger an action
checkStateChanged(). Connect to this signal if you want to trigger an action
each time the checkbox changes state. You can use isChecked() to query
whether or not a checkbox is checked.
@ -84,14 +84,22 @@ public:
setPixmap(), accel(), setAccel(), isToggleButton(), setDown(), isDown(),
isOn(), checkState(), autoRepeat(), isExclusiveToggle(), group(),
setAutoRepeat(), toggle(), pressed(), released(), clicked(), toggled(),
checkState(), and stateChanged().
checkState(), and checkStateChanged().
\sa QAbstractButton, QRadioButton
*/
/*!
\fn void QCheckBox::stateChanged(int state)
//! Qt 7: \fn void QCheckBox::stateChanged(Qt::CheckState state)
\obsolete
Use checkStateChanged(Qt::CheckState) instead.
*/
/*!
\fn void QCheckBox::checkStateChanged(Qt::CheckState state)
\since 6.7
This signal is emitted whenever the checkbox's state changes, i.e.,
whenever the user checks or unchecks it.
@ -227,6 +235,7 @@ void QCheckBox::setCheckState(Qt::CheckState state)
d->refresh();
if (state != d->publishedState) {
d->publishedState = state;
emit checkStateChanged(state);
emit stateChanged(state);
}
@ -322,6 +331,7 @@ void QCheckBox::checkStateSet()
Qt::CheckState state = checkState();
if (state != d->publishedState) {
d->publishedState = state;
emit checkStateChanged(state);
emit stateChanged(state);
}
}

View File

@ -36,11 +36,8 @@ 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
void checkStateChanged(Qt::CheckState);
protected:
bool event(QEvent *e) override;

View File

@ -25,7 +25,7 @@ private slots:
void toggle();
void pressed();
void toggled();
void stateChanged();
void checkStateChanged();
void foregroundRole();
void minimumSizeHint();
};
@ -188,34 +188,38 @@ void tst_QCheckBox::toggled()
QCOMPARE(click_count, 0);
}
void tst_QCheckBox::stateChanged()
void tst_QCheckBox::checkStateChanged()
{
QCheckBox testWidget;
QCOMPARE(testWidget.checkState(), Qt::Unchecked);
Qt::CheckState cur_state = Qt::Unchecked;
QSignalSpy checkStateChangedSpy(&testWidget, &QCheckBox::checkStateChanged);
QT_IGNORE_DEPRECATIONS(
QSignalSpy stateChangedSpy(&testWidget, &QCheckBox::stateChanged);
connect(&testWidget, &QCheckBox::stateChanged, this, [&](auto state) { cur_state = Qt::CheckState(state); });
)
connect(&testWidget, &QCheckBox::checkStateChanged, this, [&](auto state) { cur_state = state; });
testWidget.setChecked(true);
QVERIFY(QTest::qWaitFor([&]() { return stateChangedSpy.size() == 1; }));
QTRY_COMPARE(checkStateChangedSpy.size(), 1);
QCOMPARE(stateChangedSpy.size(), 1);
QCOMPARE(cur_state, Qt::Checked);
QCOMPARE(testWidget.checkState(), Qt::Checked);
testWidget.setChecked(false);
QVERIFY(QTest::qWaitFor([&]() { return stateChangedSpy.size() == 2; }));
QTRY_COMPARE(checkStateChangedSpy.size(), 2);
QCOMPARE(stateChangedSpy.size(), 2);
QCOMPARE(cur_state, Qt::Unchecked);
QCOMPARE(testWidget.checkState(), Qt::Unchecked);
testWidget.setCheckState(Qt::PartiallyChecked);
QVERIFY(QTest::qWaitFor([&]() { return stateChangedSpy.size() == 3; }));
QTRY_COMPARE(checkStateChangedSpy.size(), 3);
QCOMPARE(stateChangedSpy.size(), 3);
QCOMPARE(cur_state, Qt::PartiallyChecked);
QCOMPARE(testWidget.checkState(), Qt::PartiallyChecked);
testWidget.setCheckState(Qt::PartiallyChecked);
QCoreApplication::processEvents();
QCOMPARE(checkStateChangedSpy.size(), 3);
QCOMPARE(stateChangedSpy.size(), 3);
}