summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-08-25 12:28:11 +0200
committerLars Knoll <lars.knoll@qt.io>2020-09-02 22:44:29 +0200
commit918c61f275e8a9b46459f425df3b69961955a81d (patch)
treeee030bc5aaeda50650c5d3bf4486780e6e9405d5 /tests
parent1e1b88809261854dee47c9bc105cf71e75b75cb9 (diff)
Add support for computed properties
Add a QObjectComputedProperty. This class doesn't store the data itself, instead relies on a getter method to compute it's value. As the property is read-only, one can not bind to it, but it can be used in other property bindings. Change-Id: I0f6bffdd9f80f1d0829826f93a47257f2b3127af Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
index 2136e8e212..50c974c69c 100644
--- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
+++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
@@ -968,6 +968,7 @@ class MyQObject : public QObject
Q_PROPERTY(int foo READ foo WRITE setFoo NOTIFY fooChanged) // Use Q_BINDABLE_PROPERTY and generate iface API
Q_PROPERTY(int bar READ bar WRITE setBar NOTIFY barChanged)
Q_PROPERTY(int read READ read NOTIFY readChanged)
+ Q_PROPERTY(int computed READ computed STORED false)
signals:
void fooChanged();
@@ -985,10 +986,12 @@ public:
int bar() const { return barData.value(); }
void setBar(int i) { barData.setValue(i); }
int read() const { return readData.value(); }
+ int computed() const { return readData.value(); }
QBindable<int> bindableFoo() { return QBindable<int>(&fooData); }
QBindable<int> bindableBar() { return QBindable<int>(&barData); }
QBindable<int> bindableRead() { return QBindable<int>(&readData); }
+ QBindable<int> bindableComputed() { return QBindable<int>(&computedData); }
public:
int fooChangedCount = 0;
@@ -998,6 +1001,7 @@ public:
Q_OBJECT_BINDABLE_PROPERTY(MyQObject, int, fooData, &MyQObject::fooChanged);
Q_OBJECT_BINDABLE_PROPERTY(MyQObject, int, barData, &MyQObject::barChanged);
Q_OBJECT_BINDABLE_PROPERTY(MyQObject, int, readData, &MyQObject::readChanged);
+ Q_OBJECT_COMPUTED_PROPERTY(MyQObject, int, computedData, &MyQObject::computed);
};
void tst_QProperty::testNewStuff()
@@ -1049,6 +1053,16 @@ void tst_QProperty::testNewStuff()
QCOMPARE(object.foo(), 0);
object.readData.setValue(10);
QCOMPARE(object.foo(), 10);
+
+ QCOMPARE(object.computed(), 10);
+ object.readData.setValue(42);
+ QCOMPARE(object.computed(), 42);
+
+ object.bindableBar().setBinding(object.bindableComputed().makeBinding());
+ QCOMPARE(object.computed(), 42);
+ object.readData.setValue(111);
+ QCOMPARE(object.computed(), 111);
+
}
void tst_QProperty::qobjectObservers()