diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2023-03-06 12:04:15 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2023-03-16 14:17:24 +0100 |
commit | 566e5e1acf22ca566e9a256cab04acd1f7d06ce2 (patch) | |
tree | 026f45180be1d3a7790589744b2c61d5d65af105 /tests/auto/tools/moc | |
parent | cac1abb0126d50dc9bbb4467e00df7b31a685a2b (diff) |
moc: Allow anonymous properties
This is private for now. It's very handy to have anonymous properties
for the QML model/delegate adaptors. There are models with only
"singular" model data that doesn't have any sub-properties. Such model
data should be available from the model object via an empty string as
role. This way we can get rid of a lot of special casing.
Task-number: QTBUG-104752
Change-Id: I229e355a7cab064ee1c9f89557bc0244a5d0c90a
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/tools/moc')
-rw-r--r-- | tests/auto/tools/moc/tst_moc.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index f832612d04..e2844e29d1 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -11,6 +11,8 @@ #include <qversionnumber.h> #include <qregularexpression.h> +#include <private/qobject_p.h> + #include "using-namespaces.h" #include "assign-namespace.h" #include "no-keywords.h" @@ -704,6 +706,7 @@ private slots: void templateGtGt(); void qprivateslots(); void qprivateproperties(); + void anonymousProperties(); void warnOnPropertyWithoutREAD(); void constructors(); void typenameWithUnsigned(); @@ -1642,6 +1645,54 @@ void tst_Moc::qprivateproperties() QVERIFY(zapBindable.isBindable()); } + +class AnonymousPropertyTest1 : public QObject +{ + Q_OBJECT + QT_ANONYMOUS_PROPERTY(int READ foo WRITE setFoo) +public: + int foo() { return mFoo ; } + void setFoo(int value) { mFoo = value; } + +private: + int mFoo = 0; +}; + +class AnonymousPropertyTest2 : public QObject +{ + Q_OBJECT + QT_ANONYMOUS_PRIVATE_PROPERTY(d, int READ bar WRITE setBar) + + class MyDPointer { + public: + int bar() { return mBar ; } + void setBar(int value) { mBar = value; } + private: + int mBar = 0; + }; + +public: + AnonymousPropertyTest2(QObject *parent = nullptr) : QObject(parent), d (new MyDPointer) {} + MyDPointer *d_func() {return d.data();} + const MyDPointer *d_func() const {return d.data();} + +private: + QScopedPointer<MyDPointer> d; +}; + +void tst_Moc::anonymousProperties() +{ + AnonymousPropertyTest1 test1; + + test1.setProperty("", 17); + QCOMPARE(test1.property(""), QVariant::fromValue(17)); + + AnonymousPropertyTest2 test2; + + test2.setProperty("", 27); + QCOMPARE(test2.property(""), QVariant::fromValue(27)); +} + void tst_Moc::warnOnPropertyWithoutREAD() { #ifdef MOC_CROSS_COMPILED |