diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-06-21 11:54:18 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2022-07-08 09:38:27 +0200 |
commit | 66a30b9a33aac288335f32ea9dc8dd8542abf69f (patch) | |
tree | 08aea336362ee58df8375681b5c8b5c982076acd /tests/auto | |
parent | d56f80fd9afd31d9458da87c64a41b15a67ba3cc (diff) |
moc: Allow writing properties through bindables
BINDABLE should generally behave the same as MEMBER if "WRITE default",
except where it cannot. In particular we cannot know if any NOTIFY
signal should be sent from the synthetic WRITE accessor.
[ChangeLog][QtCore] moc will now synthesize WRITE accessors for
properties with BINDABLE if you specify "WRITE default".
Task-number: QTBUG-97249
Change-Id: I883c40ba0dda7989c840971860addaeaa75a8c83
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/tools/moc/tst_moc.cpp | 79 |
1 files changed, 58 insertions, 21 deletions
diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index 97575f5bb7..48249eec0c 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -744,7 +744,7 @@ private slots: void observerMetaCall(); void setQPRopertyBinding(); void privateQPropertyShim(); - void readThroughBindable(); + void readWriteThroughBindable(); signals: void sigWithUnsignedArg(unsigned foo); @@ -4339,7 +4339,7 @@ void tst_Moc::privateQPropertyShim() class BindableOnly : public QObject { Q_OBJECT - Q_PROPERTY(int score BINDABLE scoreBindable READ default) + Q_PROPERTY(int score BINDABLE scoreBindable READ default WRITE default) public: BindableOnly(QObject *parent = nullptr) : QObject(parent) @@ -4350,27 +4350,64 @@ private: QProperty<int> m_score; }; - -void tst_Moc::readThroughBindable() +class BindableAndNotifyable : public QObject { - BindableOnly o; - - QCOMPARE(o.scoreBindable().value(), 4); - QCOMPARE(o.property("score").toInt(), 4); - o.scoreBindable().setValue(5); - QCOMPARE(o.scoreBindable().value(), 5); - QCOMPARE(o.property("score").toInt(), 5); - - - const QMetaObject *mo = o.metaObject(); - const int i = mo->indexOfProperty("score"); - QVERIFY(i > 0); - - QMetaProperty p = mo->property(i); - QCOMPARE(p.name(), "score"); + Q_OBJECT + Q_PROPERTY(int score BINDABLE scoreBindable NOTIFY scoreChanged READ default WRITE default) +public: + BindableAndNotifyable(QObject *parent = nullptr) + : QObject(parent) + , m_score(4) + {} + QBindable<int> scoreBindable() { return QBindable<int>(&m_score); } +signals: + void scoreChanged(); +private: + QProperty<int> m_score; +}; - QVERIFY(p.isValid()); - QCOMPARE(p.read(&o), 5); +void tst_Moc::readWriteThroughBindable() +{ + { + BindableOnly o; + QCOMPARE(o.scoreBindable().value(), 4); + QCOMPARE(o.property("score").toInt(), 4); + o.scoreBindable().setValue(5); + QCOMPARE(o.scoreBindable().value(), 5); + QCOMPARE(o.property("score").toInt(), 5); + const QMetaObject *mo = o.metaObject(); + const int i = mo->indexOfProperty("score"); + QVERIFY(i > 0); + QMetaProperty p = mo->property(i); + QCOMPARE(p.name(), "score"); + QVERIFY(p.isValid()); + QVERIFY(p.isWritable()); + QCOMPARE(p.read(&o), 5); + QVERIFY(o.setProperty("score", 6)); + QCOMPARE(o.property("score").toInt(), 6); + QVERIFY(p.write(&o, 7)); + QCOMPARE(p.read(&o), 7); + } + { + BindableAndNotifyable o; + QCOMPARE(o.scoreBindable().value(), 4); + QCOMPARE(o.property("score").toInt(), 4); + o.scoreBindable().setValue(5); + QCOMPARE(o.scoreBindable().value(), 5); + QCOMPARE(o.property("score").toInt(), 5); + const QMetaObject *mo = o.metaObject(); + const int i = mo->indexOfProperty("score"); + QVERIFY(i > 0); + QMetaProperty p = mo->property(i); + QCOMPARE(p.name(), "score"); + QVERIFY(p.isValid()); + QVERIFY(p.isWritable()); + QCOMPARE(p.read(&o), 5); + QVERIFY(o.setProperty("score", 6)); + QCOMPARE(o.property("score").toInt(), 6); + QVERIFY(p.write(&o, 7)); + QCOMPARE(p.read(&o), 7); + } } QTEST_MAIN(tst_Moc) |