From a1a2d97e34eb7e2445877cf9e557db55a3540f9d Mon Sep 17 00:00:00 2001 From: Andreas Buhr Date: Thu, 28 Jan 2021 11:00:08 +0100 Subject: [PATCH] Remove QObjectCompatProperty::operator= for safer usage Introduction of QObjectCompatProperty requires every write to the property to be examined whether it is OK or should be replaced by a setValueBypassingBindings/markDirty combination. The existence of operator= make this difficult as it is easy to miss places where it is written. By not having operator=, we can help developers make sure they had a conscious decision about each write to the property. Change-Id: Ia61ea4722eb0bab26ce7684b85dd03d710cd1751 Reviewed-by: Fabian Kosmale --- src/corelib/animation/qpauseanimation.cpp | 2 +- src/corelib/kernel/qproperty_p.h | 6 ----- src/gui/image/qmovie.cpp | 2 +- .../kernel/qproperty/tst_qproperty.cpp | 22 +++++++++---------- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/corelib/animation/qpauseanimation.cpp b/src/corelib/animation/qpauseanimation.cpp index d6fa0eb210f..31802f094dc 100644 --- a/src/corelib/animation/qpauseanimation.cpp +++ b/src/corelib/animation/qpauseanimation.cpp @@ -129,7 +129,7 @@ void QPauseAnimation::setDuration(int msecs) return; } Q_D(QPauseAnimation); - d->duration = msecs; + d->duration.setValue(msecs); } QBindable QPauseAnimation::bindableDuration() diff --git a/src/corelib/kernel/qproperty_p.h b/src/corelib/kernel/qproperty_p.h index 27cb8c907fc..f4fee1befba 100644 --- a/src/corelib/kernel/qproperty_p.h +++ b/src/corelib/kernel/qproperty_p.h @@ -461,12 +461,6 @@ public: notify(bd); } - QObjectCompatProperty &operator=(parameter_type newValue) - { - setValue(newValue); - return *this; - } - QPropertyBinding setBinding(const QPropertyBinding &newBinding) { QtPrivate::QPropertyBindingData *bd = qGetBindingStorage(owner())->bindingData(this, true); diff --git a/src/gui/image/qmovie.cpp b/src/gui/image/qmovie.cpp index 0e1cf4481e2..a98a4ed154b 100644 --- a/src/gui/image/qmovie.cpp +++ b/src/gui/image/qmovie.cpp @@ -929,7 +929,7 @@ void QMovie::setSpeed(int percentSpeed) Q_D(QMovie); if (!d->speed && d->movieState == Running) d->nextImageTimer.start(nextFrameDelay()); - d->speed = percentSpeed; + d->speed.setValue(percentSpeed); } int QMovie::speed() const diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp index 630eee9e9cf..6062c231892 100644 --- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp +++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp @@ -469,12 +469,12 @@ class BindingLoopTester : public QObject BindingLoopTester() {} int eagerProp() {return eagerData.value();} - void setEagerProp(int i) { eagerData = i; } + void setEagerProp(int i) { eagerData.setValue(i); } QBindable bindableEagerProp() {return QBindable(&eagerData);} Q_OBJECT_COMPAT_PROPERTY(BindingLoopTester, int, eagerData, &BindingLoopTester::setEagerProp) int eagerProp2() {return eagerData2.value();} - void setEagerProp2(int i) { eagerData2 = i; } + void setEagerProp2(int i) { eagerData2.setValue(i); } QBindable bindableEagerProp2() {return QBindable(&eagerData2);} Q_OBJECT_COMPAT_PROPERTY(BindingLoopTester, int, eagerData2, &BindingLoopTester::setEagerProp2) }; @@ -536,7 +536,7 @@ public: #define GEN(N) \ int prop##N() {return propData##N.value();} \ - void setProp##N(int i) { propData##N = i; } \ + void setProp##N(int i) { propData##N.setValue(i); } \ QBindable bindableProp##N() {return QBindable(&propData##N);} \ Q_OBJECT_COMPAT_PROPERTY(ReallocTester, int, propData##N, &ReallocTester::setProp##N) GEN(1) @@ -1063,7 +1063,7 @@ public: ++setCompatCalled; if (i < 0) i = 0; - compatData = i; + compatData.setValue(i); emit compatChanged(); } @@ -1201,7 +1201,7 @@ void tst_QProperty::compatBindings() QCOMPARE(object.compatData, 0); // setting data through the private interface should not call the changed signal or the public setter - object.compatData = 10; + object.compatData.setValue(10); QCOMPARE(object.compatChangedCount, 0); QCOMPARE(object.setCompatCalled, 0); // going through the public API should emit the signal @@ -1390,7 +1390,7 @@ class CompatPropertyTester : public QObject CompatPropertyTester(QObject *parent = nullptr) : QObject(parent) { } int prop1() {return prop1Data.value();} - void setProp1(int i) { prop1Data = i; } + void setProp1(int i) { prop1Data.setValue(i); } QBindable bindableProp1() {return QBindable(&prop1Data);} Q_OBJECT_COMPAT_PROPERTY(CompatPropertyTester, int, prop1Data, &CompatPropertyTester::setProp1) @@ -1421,9 +1421,9 @@ signals: void prop3Changed(); public: - void setProp1(int val) { prop1Data = val; emit prop1Changed();} - void setProp2(int val) { prop2Data = val; emit prop2Changed();} - void setProp3(int val) { prop3Data = val; emit prop3Changed();} + void setProp1(int val) { prop1Data.setValue(val); emit prop1Changed();} + void setProp2(int val) { prop2Data.setValue(val); emit prop2Changed();} + void setProp3(int val) { prop3Data.setValue(val); emit prop3Changed();} int prop1() { return prop1Data; } int prop2() { return prop2Data; } @@ -1496,7 +1496,7 @@ public: QBindable bindableProp2() { return QBindable(&prop2Data); } CustomType prop3() { return prop3Data.value(); } - void setProp3(CustomType val) { prop3Data = val; } + void setProp3(CustomType val) { prop3Data.setValue(val); } QBindable bindableProp3() { return QBindable(&prop3Data); } Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(PropertyWithInitializationTester, int, prop1Data, 5, @@ -1533,7 +1533,7 @@ public: QBindable bindableValue1() {return { &m_value1 };} int value2() const {return m_value2;} - void setValue2(int val) {m_value2 = val;} + void setValue2(int val) { m_value2.setValue(val); } QBindable bindableValue2() {return { &m_value2 };} int computed() const { return staticValue + m_value1; }