Prepare QGuiAction::checked property for declarative use

Make the order checkable and checked are set in insignificant, by
storing ignored checked value for un-checkable actions.

Also gives checkable its own changed signal.

Change-Id: If03db7c92481a542b6220604860abddb322bb517
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
bb10
Allan Sandfeld Jensen 2020-02-17 16:27:11 +01:00
parent 75c0ffaf6d
commit e5e969c00c
3 changed files with 55 additions and 6 deletions

View File

@ -802,8 +802,12 @@ void QGuiAction::setCheckable(bool b)
return;
d->checkable = b;
d->checked = false;
QPointer<QGuiAction> guard(this);
d->sendDataChanged();
if (guard)
emit checkableChanged(b);
if (guard && d->checked)
emit toggled(b);
}
bool QGuiAction::isCheckable() const
@ -839,11 +843,13 @@ void QGuiAction::toggle()
void QGuiAction::setChecked(bool b)
{
Q_D(QGuiAction);
if (!d->checkable || d->checked == b)
if (d->checked == b)
return;
QPointer<QGuiAction> guard(this);
d->checked = b;
if (!d->checkable)
return;
QPointer<QGuiAction> guard(this);
d->sendDataChanged();
if (guard)
emit toggled(b);
@ -852,7 +858,7 @@ void QGuiAction::setChecked(bool b)
bool QGuiAction::isChecked() const
{
Q_D(const QGuiAction);
return d->checked;
return d->checked && d->checkable;
}
/*!

View File

@ -61,7 +61,7 @@ class Q_GUI_EXPORT QGuiAction : public QObject
Q_OBJECT
Q_DECLARE_PRIVATE(QGuiAction)
Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable NOTIFY changed)
Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable NOTIFY checkableChanged FINAL)
Q_PROPERTY(bool checked READ isChecked WRITE setChecked NOTIFY toggled)
Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged RESET resetEnabled FINAL)
Q_PROPERTY(QIcon icon READ icon WRITE setIcon NOTIFY changed)
@ -181,7 +181,8 @@ public Q_SLOTS:
Q_SIGNALS:
void changed();
void enabledChanged(bool changed);
void enabledChanged(bool enabled);
void checkableChanged(bool checkable);
void triggered(bool checked = false);
void hovered();
void toggled(bool);

View File

@ -57,6 +57,7 @@ private slots:
void task229128TriggeredSignalWithoutActiongroup();
void setData();
void setEnabledSetVisible();
void setCheckabledSetChecked();
private:
const int m_keyboardScheme;
@ -231,5 +232,46 @@ void tst_QGuiAction::setEnabledSetVisible()
QCOMPARE(spy.count(), 2);
}
void tst_QGuiAction::setCheckabledSetChecked()
{
QGuiAction action(nullptr);
QSignalSpy changedSpy(&action, &QGuiAction::changed);
QSignalSpy checkedSpy(&action, &QGuiAction::toggled);
QSignalSpy checkableSpy(&action, &QGuiAction::checkableChanged);
QVERIFY(!action.isCheckable());
QVERIFY(!action.isChecked());
QCOMPARE(changedSpy.count(), 0);
QCOMPARE(checkedSpy.count(), 0);
QCOMPARE(checkableSpy.count(), 0);
action.setCheckable(true);
QVERIFY(action.isCheckable());
QVERIFY(!action.isChecked());
QCOMPARE(changedSpy.count(), 1);
QCOMPARE(checkedSpy.count(), 0);
QCOMPARE(checkableSpy.count(), 1);
action.setChecked(true);
QVERIFY(action.isCheckable());
QVERIFY(action.isChecked());
QCOMPARE(changedSpy.count(), 2);
QCOMPARE(checkedSpy.count(), 1);
QCOMPARE(checkableSpy.count(), 1);
action.setCheckable(false);
QVERIFY(!action.isCheckable());
QVERIFY(!action.isChecked());
QCOMPARE(changedSpy.count(), 3);
QCOMPARE(checkedSpy.count(), 2);
QCOMPARE(checkableSpy.count(), 2);
action.setCheckable(true);
QVERIFY(action.isCheckable());
QVERIFY(action.isChecked());
QCOMPARE(changedSpy.count(), 4);
QCOMPARE(checkedSpy.count(), 3);
QCOMPARE(checkableSpy.count(), 3);
}
QTEST_MAIN(tst_QGuiAction)
#include "tst_qguiaction.moc"