From f0668433c4041a00399eeb29856273fce1389bf3 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Fri, 11 Dec 2020 11:56:48 +0100 Subject: Bindable property with initialization Implement Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS and Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS macros. They allow to directly initialize the property member. Task-number: QTBUG-85520 Change-Id: I76541d6785bbbf27976b9f0b865fb45be1e9beee Reviewed-by: Fabian Kosmale --- .../corelib/kernel/qproperty/tst_qproperty.cpp | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp index ca2de478cf..2ff32d0d1a 100644 --- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp +++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp @@ -89,6 +89,8 @@ private slots: void compatPropertyNoDobuleNotification(); void noFakeDependencies(); + + void bindablePropertyWithInitialization(); }; void tst_QProperty::functorBinding() @@ -1416,6 +1418,68 @@ void tst_QProperty::noFakeDependencies() QCOMPARE(old, bindingFunctionCalled); } +struct CustomType +{ + CustomType() = default; + CustomType(int val) : value(val) { } + CustomType(int val, int otherVal) : value(val), anotherValue(otherVal) { } + CustomType(const CustomType &) = default; + CustomType(CustomType &&) = default; + ~CustomType() = default; + CustomType &operator=(const CustomType &) = default; + CustomType &operator=(CustomType &&) = default; + bool operator==(const CustomType &other) const + { + return (value == other.value) && (anotherValue == other.anotherValue); + } + + int value = 0; + int anotherValue = 0; +}; + +class PropertyWithInitializationTester : public QObject +{ + Q_OBJECT + Q_PROPERTY(int prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed BINDABLE bindableProp1) + Q_PROPERTY(CustomType prop2 READ prop2 WRITE setProp2 BINDABLE bindableProp2) + Q_PROPERTY(CustomType prop3 READ prop3 WRITE setProp3 BINDABLE bindableProp3) +signals: + void prop1Changed(); + +public: + PropertyWithInitializationTester(QObject *parent = nullptr) : QObject(parent) { } + + int prop1() { return prop1Data.value(); } + void setProp1(int i) { prop1Data = i; } + QBindable bindableProp1() { return QBindable(&prop1Data); } + + CustomType prop2() { return prop2Data.value(); } + void setProp2(CustomType val) { prop2Data = val; } + QBindable bindableProp2() { return QBindable(&prop2Data); } + + CustomType prop3() { return prop3Data.value(); } + void setProp3(CustomType val) { prop3Data = val; } + QBindable bindableProp3() { return QBindable(&prop3Data); } + + Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(PropertyWithInitializationTester, int, prop1Data, 5, + &PropertyWithInitializationTester::prop1Changed) + Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(PropertyWithInitializationTester, CustomType, prop2Data, + CustomType(5)) + Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(PropertyWithInitializationTester, CustomType, prop3Data, + &PropertyWithInitializationTester::setProp3, + CustomType(10, 20)) +}; + +void tst_QProperty::bindablePropertyWithInitialization() +{ + PropertyWithInitializationTester tester; + + QCOMPARE(tester.prop1(), 5); + QCOMPARE(tester.prop2().value, 5); + QCOMPARE(tester.prop3().value, 10); + QCOMPARE(tester.prop3().anotherValue, 20); +} + QTEST_MAIN(tst_QProperty); #include "tst_qproperty.moc" -- cgit v1.2.3