diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-06-09 13:23:33 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2022-06-22 16:05:07 +0200 |
commit | 2c0518eb62ebe475abf84b8ee2fd50c53720d3a4 (patch) | |
tree | 1e729142d25a1452d4be02fd743180ddc22f307e /tests | |
parent | 073214fdf943e1a0beb660a039e39dc1ea43836e (diff) |
moc: Allow reading property values through bindables
The behavior is similar to MEMBER: If the READ accessor is "default",
synthesize it using the bindable.
[ChangeLog][QtCore] You can now specify "READ default" in a Q_PROPERTY
if you also specify a BINDABLE. moc will synthesize a READ accessor in
that case.
Task-number: QTBUG-97249
Change-Id: I4a275adabaed12df95dac505095f847c4be65dfe
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/tools/moc/tst_moc.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index f56e2c3024..89794ecc1d 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -743,6 +743,7 @@ private slots: void observerMetaCall(); void setQPRopertyBinding(); void privateQPropertyShim(); + void readThroughBindable(); signals: void sigWithUnsignedArg(unsigned foo); @@ -4329,6 +4330,44 @@ void tst_Moc::privateQPropertyShim() QCOMPARE(testObject.priv.testProperty2.value(), 42); } + +class BindableOnly : public QObject +{ + Q_OBJECT + Q_PROPERTY(int score BINDABLE scoreBindable READ default) +public: + BindableOnly(QObject *parent = nullptr) + : QObject(parent) + , m_score(4) + {} + QBindable<int> scoreBindable() { return QBindable<int>(&m_score); } +private: + QProperty<int> m_score; +}; + + +void tst_Moc::readThroughBindable() +{ + 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()); + QCOMPARE(p.read(&o), 5); +} + QTEST_MAIN(tst_Moc) // the generated code must compile with QT_NO_KEYWORDS |