diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2021-02-26 09:01:25 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2021-03-01 09:15:24 +0100 |
commit | 62b7832512579625a917d82141d09673c7db0d76 (patch) | |
tree | 5357249e33942a833f6e03f41b025a04ea64a131 /tests | |
parent | c0bc7b241b18926338179dcf492699e7387236f9 (diff) |
Use the correct metaObject in captureProperty()
QObject::staticMetaObject is not very useful.
Change-Id: Ifc40e1fa08755c59ff6b8ae23a7a1257f34507da
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 89ebac46d7bde1df265b8970132bf09dc790eca2)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qqmlengine/tst_qqmlengine.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp index 6c00494189..c1239265ac 100644 --- a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp +++ b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp @@ -85,6 +85,7 @@ private slots: void cachedGetterLookup_qtbug_75335(); void createComponentOnSingletonDestruction(); void uiLanguage(); + void captureQProperty(); public slots: QObject *createAQObjectForOwnershipTest () @@ -1238,6 +1239,78 @@ void tst_qqmlengine::uiLanguage() } } +class WithQProperty : public QObject +{ + Q_OBJECT + Q_PROPERTY(int foo READ foo WRITE setFoo BINDABLE fooBindable) + +public: + WithQProperty(QObject *parent = nullptr) : QObject(parent) { m_foo.setValue(12); } + + int foo() const { return m_foo.value(); } + void setFoo(int foo) { m_foo.setValue(foo); } + QBindable<int> fooBindable() { return QBindable<int>(&m_foo); } + + int getFooWithCapture() + { + const QMetaObject *m = metaObject(); + currentEngine->captureProperty(this, m->property(m->indexOfProperty("foo"))); + return m_foo.value(); + } + + static QQmlEngine *currentEngine; + +private: + QProperty<int> m_foo; +}; + +class WithoutQProperty : public QObject +{ + Q_OBJECT + Q_PROPERTY(int foo READ foo WRITE setFoo NOTIFY fooChanged) +public: + WithoutQProperty(QObject *parent = nullptr) : QObject(parent), m_foo(new WithQProperty(this)) {} + + int foo() const { return m_foo->getFooWithCapture(); } + + void setFoo(int foo) { + if (foo != m_foo->foo()) { + m_foo->setFoo(foo); + emit fooChanged(); + } + } + + void triggerBinding(int val) + { + m_foo->setFoo(val); + } + +signals: + void fooChanged(); + +private: + WithQProperty *m_foo; +}; + +QQmlEngine *WithQProperty::currentEngine = nullptr; + +void tst_qqmlengine::captureQProperty() +{ + qmlRegisterType<WithoutQProperty>("Foo", 1, 0, "WithoutQProperty"); + QQmlEngine engine; + WithQProperty::currentEngine = &engine; + QQmlComponent c(&engine); + c.setData("import Foo\n" + "WithoutQProperty {\n" + " property int x: foo\n" + "}", QUrl()); + QVERIFY2(c.isReady(), c.errorString().toUtf8()); + QScopedPointer<QObject> o(c.create()); + QCOMPARE(o->property("x").toInt(), 12); + static_cast<WithoutQProperty *>(o.data())->triggerBinding(13); + QCOMPARE(o->property("x").toInt(), 13); +} + QTEST_MAIN(tst_qqmlengine) #include "tst_qqmlengine.moc" |