diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-07-07 16:54:44 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-07-08 11:01:38 +0200 |
commit | 0cd3820e4d62d91c006a807f04c77166b2dbc7f5 (patch) | |
tree | c2045a95f28b40dd2196f26637b57d330ffbbfb6 /tests/auto/tools/moc/tst_moc.cpp | |
parent | eacf83594a5bf063665fdecc6c438673dc9f2cf6 (diff) |
moc: Allow out-of-line storage for Q_PRIVATE_QPROPERTY
If you pass "STORED false" the name is interpreted as function to be
invoked in order to access the property. This allows storage of a
property in a lazily allocated data type.
Change-Id: I4d3a9cac6985c6419ce687868cb74b91921595a6
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/tools/moc/tst_moc.cpp')
-rw-r--r-- | tests/auto/tools/moc/tst_moc.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index 01d207b080..14c61d552d 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -4170,13 +4170,17 @@ class ClassWithPrivateQPropertyShim :public QObject Q_OBJECT public: Q_PRIVATE_QPROPERTY(d_func(), int, testProperty, setTestProperty, NOTIFY testPropertyChanged) + Q_PRIVATE_QPROPERTY(d_func(), int, lazyTestProperty, setLazyTestProperty, + NOTIFY lazyTestPropertyChanged STORED false) Q_PRIVATE_QPROPERTIES_BEGIN Q_PRIVATE_QPROPERTY_IMPL(testProperty) + Q_PRIVATE_QPROPERTY_IMPL(lazyTestProperty) Q_PRIVATE_QPROPERTIES_END signals: void testPropertyChanged(); + void lazyTestPropertyChanged(); public: struct Private { @@ -4188,6 +4192,22 @@ public: void onTestPropertyChanged() { q->testPropertyChanged(); } QNotifiedProperty<int, &Private::onTestPropertyChanged> testProperty; + + void onLazyTestPropertyChanged() { q->lazyTestPropertyChanged(); } + + QNotifiedProperty<int, &Private::onLazyTestPropertyChanged> &lazyTestProperty() { + if (!lazyTestPropertyStorage) + lazyTestPropertyStorage.reset(new QNotifiedProperty<int, &Private::onLazyTestPropertyChanged>); + return *lazyTestPropertyStorage; + } + + const QNotifiedProperty<int, &Private::onLazyTestPropertyChanged> &lazyTestProperty() const { + if (!lazyTestPropertyStorage) + lazyTestPropertyStorage.reset(new QNotifiedProperty<int, &Private::onLazyTestPropertyChanged>); + return *lazyTestPropertyStorage; + } + + mutable QScopedPointer<QNotifiedProperty<int, &Private::onLazyTestPropertyChanged>> lazyTestPropertyStorage; }; Private priv{this}; @@ -4220,6 +4240,24 @@ void tst_Moc::privateQPropertyShim() testObject.setTestProperty(400); QVERIFY(!testObject.testProperty.hasBinding()); QCOMPARE(testObject.testProperty(), 400); + + // Created and default-initialized, without nullptr access + QCOMPARE(testObject.lazyTestProperty(), 0); + + // Explicitly set to something + testObject.priv.lazyTestProperty().setValue(&testObject.priv, 42); + QCOMPARE(testObject.property("lazyTestProperty").toInt(), 42); + + // Behave like a QProperty + QVERIFY(!testObject.lazyTestProperty.hasBinding()); + testObject.lazyTestProperty.setBinding([]() { return 100; }); + QCOMPARE(testObject.lazyTestProperty.value(), 100); + QVERIFY(testObject.lazyTestProperty.hasBinding()); + + // Old style setter getters + testObject.setLazyTestProperty(400); + QVERIFY(!testObject.lazyTestProperty.hasBinding()); + QCOMPARE(testObject.lazyTestProperty(), 400); } QTEST_MAIN(tst_Moc) |