diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-08-22 17:21:36 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-09-02 22:44:29 +0200 |
commit | ad32ac5b4f05c9eed1fb7a93ee7947050d840a19 (patch) | |
tree | 5f14ae7a6a588ad3c9400058943f675556a403a9 /tests/auto/corelib | |
parent | 3e6c09279304fbde1860288717958e28377b9a9c (diff) |
Make bindings introspectable through moc
Add a new BINDABLE declaration to the Q_PROPERTY() macro that tells moc
where to find the QBindable for the property.
Add a QUntypedBindable base class to QBindable<T> that gives access to
generic functionality and checks argument compatibility at runtime.
QBindable<T> will still do static checking at compile time.
Add QMetaProperty::isBindable() and QMetaProperty::bindable()
to be able to dynamically access the binding functionality.
Change-Id: Ic7b08ae2cde83fd43e627d813a886e1de01fa3dc
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r-- | tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp index b00adc6620..f5652eb599 100644 --- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp +++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp @@ -77,6 +77,7 @@ private slots: void testNewStuff(); void qobjectObservers(); void compatBindings(); + void metaProperty(); }; void tst_QProperty::functorBinding() @@ -966,8 +967,8 @@ void tst_QProperty::bindingValueReplacement() class MyQObject : public QObject { Q_OBJECT - 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 foo READ foo WRITE setFoo BINDABLE bindableFoo NOTIFY fooChanged) + Q_PROPERTY(int bar READ bar WRITE setBar BINDABLE bindableBar NOTIFY barChanged) Q_PROPERTY(int read READ read NOTIFY readChanged) Q_PROPERTY(int computed READ computed STORED false) Q_PROPERTY(int compat READ compat WRITE setCompat NOTIFY compatChanged) @@ -1162,6 +1163,53 @@ void tst_QProperty::compatBindings() QCOMPARE(object.setCompatCalled, 4); } +void tst_QProperty::metaProperty() +{ + MyQObject object; + QObject::connect(&object, &MyQObject::fooChanged, &object, &MyQObject::fooHasChanged); + QObject::connect(&object, &MyQObject::barChanged, &object, &MyQObject::barHasChanged); + QObject::connect(&object, &MyQObject::compatChanged, &object, &MyQObject::compatHasChanged); + + QCOMPARE(object.fooChangedCount, 0); + object.setFoo(10); + QCOMPARE(object.fooChangedCount, 1); + QCOMPARE(object.foo(), 10); + + auto f = [&object]() -> int { + return object.barData; + }; + QCOMPARE(object.barChangedCount, 0); + object.setBar(42); + QCOMPARE(object.barChangedCount, 1); + QCOMPARE(object.fooChangedCount, 1); + int fooIndex = object.metaObject()->indexOfProperty("foo"); + QVERIFY(fooIndex >= 0); + QMetaProperty fooProp = object.metaObject()->property(fooIndex); + QVERIFY(fooProp.isValid()); + auto fooBindable = fooProp.bindable(&object); + QVERIFY(fooBindable.isValid()); + QVERIFY(fooBindable.isBindable()); + QVERIFY(!fooBindable.hasBinding()); + fooBindable.setBinding(Qt::makePropertyBinding(f)); + QVERIFY(fooBindable.hasBinding()); + QCOMPARE(object.fooChangedCount, 2); + QCOMPARE(object.fooData.value(), 42); + object.setBar(666); + QCOMPARE(object.fooChangedCount, 3); + QCOMPARE(object.barChangedCount, 2); + QCOMPARE(object.fooData.value(), 666); + QCOMPARE(object.fooChangedCount, 3); + + fooBindable.setBinding(QUntypedPropertyBinding()); + QVERIFY(!fooBindable.hasBinding()); + QCOMPARE(object.fooData.value(), 666); + + object.setBar(0); + QCOMPARE(object.fooData.value(), 666); + object.setFoo(1); + QCOMPARE(object.fooData.value(), 1); +} + QTEST_MAIN(tst_QProperty); #include "tst_qproperty.moc" |