From dff7689a016d63fbaf1abc6f00a768dbfb8ec095 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Mon, 23 Mar 2020 21:32:38 +0100 Subject: Add support for binding directly to QProperty instances Avoid going through externally managed bindings and instead allocate a more lightweight property binding. It's basically a QQmlJavaScriptExpression and one pointer plus the overhead of QPropertyBindingPrivate. Change-Id: I1530330926d351b61f2b3bbad39301c628a8bef1 Reviewed-by: Fabian Kosmale --- .../auto/qml/qqmlecmascript/data/bindingOnQProperty.qml | 7 +++++++ tests/auto/qml/qqmlecmascript/testtypes.cpp | 2 ++ tests/auto/qml/qqmlecmascript/testtypes.h | 9 +++++++++ tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 17 +++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 tests/auto/qml/qqmlecmascript/data/bindingOnQProperty.qml (limited to 'tests/auto/qml/qqmlecmascript') diff --git a/tests/auto/qml/qqmlecmascript/data/bindingOnQProperty.qml b/tests/auto/qml/qqmlecmascript/data/bindingOnQProperty.qml new file mode 100644 index 0000000000..1bd043d9df --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/bindingOnQProperty.qml @@ -0,0 +1,7 @@ +import Qt.test 1.0 +ClassWithQProperty { + property int externalValue + value: { + return externalValue + } +} diff --git a/tests/auto/qml/qqmlecmascript/testtypes.cpp b/tests/auto/qml/qqmlecmascript/testtypes.cpp index 89887fe6c3..606860379e 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.cpp +++ b/tests/auto/qml/qqmlecmascript/testtypes.cpp @@ -548,6 +548,8 @@ void registerTypes() qmlRegisterType("Qt.test", 1, 0, "FloatingQObject"); qmlRegisterType("Qt.test", 1, 0, "ClashingNames"); + + qmlRegisterType("Qt.test", 1, 0, "ClassWithQProperty"); } #include "testtypes.moc" diff --git a/tests/auto/qml/qqmlecmascript/testtypes.h b/tests/auto/qml/qqmlecmascript/testtypes.h index 03e3262e7b..415e884eb8 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.h +++ b/tests/auto/qml/qqmlecmascript/testtypes.h @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -1726,6 +1727,14 @@ public: Q_INVOKABLE bool clashes() const { return true; } }; +struct ClassWithQProperty : public QObject +{ + Q_OBJECT + Q_PROPERTY(int value) +public: + QProperty value; +}; + void registerTypes(); #endif // TESTTYPES_H diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index b3e4296a13..fbb344f9a0 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -381,6 +381,7 @@ private slots: void getThisObject(); void semicolonAfterProperty(); void hugeStack(); + void bindingOnQProperty(); void gcCrashRegressionTest(); @@ -9281,6 +9282,22 @@ void tst_qqmlecmascript::gcCrashRegressionTest() QCOMPARE(process.exitCode(), 0); } +void tst_qqmlecmascript::bindingOnQProperty() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("bindingOnQProperty.qml")); + QVERIFY2(component.isReady(), qPrintable(component.errorString())); + QScopedPointer test(component.create()); + test->setProperty("externalValue", 42); + QCOMPARE(test->property("value").toInt(), 42); + test->setProperty("externalValue", 100); + QCOMPARE(test->property("value").toInt(), 100); + + QVERIFY(qobject_cast(test.data())); + QProperty &qprop = static_cast(test.data())->value; + QVERIFY(qprop.hasBinding()); +} + QTEST_MAIN(tst_qqmlecmascript) #include "tst_qqmlecmascript.moc" -- cgit v1.2.3