diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2020-04-15 20:23:28 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2020-04-30 19:39:25 +0200 |
commit | 3d7265db9075db7b22b241b659f23d392d62d804 (patch) | |
tree | 902bb831d91f39b89d4481dca9381a18abb5dac3 /tests/auto/tools | |
parent | b480acb3720c0d61c5c69a2b861af63b9d7c9f86 (diff) |
Provide a way of exposing private QProperties with a fake API
The API reduces the amount of manual plumbing required to offer a
conceptual property through the traditional setter/getter API as well as
through QProperty<T> API. Since the latter would require inlining the
type and thus making it impossible to add new properties without
breaking binary compatibility, this patch introduces a fake API that
behaves similar but does not contain the property by value.
Change-Id: Ib9bccd867f0e4e36a520e5583ba348e728284253
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/tools')
-rw-r--r-- | tests/auto/tools/moc/tst_moc.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index f3cc21f502..2fe5f42609 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -727,6 +727,7 @@ private slots: void qpropertyMembers(); void observerMetaCall(); void setQPRopertyBinding(); + void privateQPropertyShim(); signals: void sigWithUnsignedArg(unsigned foo); @@ -4212,6 +4213,65 @@ void tst_Moc::setQPRopertyBinding() QVERIFY(bindingCalled); // but now it should've been called :) } + +class ClassWithPrivateQPropertyShim :public QObject +{ + Q_OBJECT +public: + Q_PRIVATE_QPROPERTY(d_func(), int, testProperty, setTestProperty, NOTIFY testPropertyChanged) + + Q_PRIVATE_QPROPERTIES_BEGIN + Q_PRIVATE_QPROPERTY_IMPL(testProperty) + Q_PRIVATE_QPROPERTIES_END + +signals: + void testPropertyChanged(); +public: + + struct Private { + Private(ClassWithPrivateQPropertyShim *pub) + : q(pub) + {} + + ClassWithPrivateQPropertyShim *q = nullptr; + + QProperty<int> testProperty; + void onTestPropertyChanged() { q->testPropertyChanged(); } + QPropertyMemberChangeHandler<&Private::testProperty, &Private::onTestPropertyChanged> testChangeHandler{this}; + }; + Private priv{this}; + + Private *d_func() { return &priv; } + const Private *d_func() const { return &priv; } +}; + + +void tst_Moc::privateQPropertyShim() +{ + ClassWithPrivateQPropertyShim testObject; + + { + auto metaObject = &ClassWithPrivateQPropertyShim::staticMetaObject; + QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("testProperty")); + QVERIFY(prop.isValid()); + QVERIFY(prop.notifySignal().isValid()); + } + + testObject.priv.testProperty.setValue(42); + QCOMPARE(testObject.property("testProperty").toInt(), 42); + + // Behave like a QProperty + QVERIFY(!testObject.testProperty.hasBinding()); + testObject.testProperty.setBinding([]() { return 100; }); + QCOMPARE(testObject.testProperty.value(), 100); + QVERIFY(testObject.testProperty.hasBinding()); + + // Old style setter getters + testObject.setTestProperty(400); + QVERIFY(!testObject.testProperty.hasBinding()); + QCOMPARE(testObject.testProperty(), 400); +} + QTEST_MAIN(tst_Moc) // the generated code must compile with QT_NO_KEYWORDS |