From 681725dbb132ca7ea3ac91ea643d556bfb8b60ac Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Fri, 26 Feb 2021 09:01:25 +0100 Subject: Use the correct metaObject in captureProperty() QObject::staticMetaObject is not very useful. Change-Id: Ifc40e1fa08755c59ff6b8ae23a7a1257f34507da Reviewed-by: Fabian Kosmale (cherry picked from commit 89ebac46d7bde1df265b8970132bf09dc790eca2) --- tests/auto/qml/qqmlengine/tst_qqmlengine.cpp | 73 ++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'tests/auto/qml') 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 fooBindable() { return QBindable(&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 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("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 o(c.create()); + QCOMPARE(o->property("x").toInt(), 12); + static_cast(o.data())->triggerBinding(13); + QCOMPARE(o->property("x").toInt(), 13); +} + QTEST_MAIN(tst_qqmlengine) #include "tst_qqmlengine.moc" -- cgit v1.2.3