From 0deb3961e6b0a1f15a491f7a19085df3a7a506b1 Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Thu, 21 Jan 2021 20:02:17 +0100 Subject: QmlBind: support bindable properties This patch ensures that the QML Binding element can also save and restore C++ bindings. Should QQuickItem's x and y property be ported to the new property system, we'd need new test cases to verify that "old-style" bindings are still handled correctly. This task is however left for the change porting the properties. Task-number: QTBUG-90493 Change-Id: I506ffa1060ff32a7d722214e5ccd469bdaa61ff8 Reviewed-by: Ulf Hermann --- tests/auto/qml/qqmlbinding/CMakeLists.txt | 9 ++++ .../auto/qml/qqmlbinding/WithBindableProperties.h | 55 ++++++++++++++++++++++ .../auto/qml/qqmlbinding/data/restoreBinding5.qml | 24 ++++++++++ tests/auto/qml/qqmlbinding/tst_qqmlbinding.cpp | 30 ++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 tests/auto/qml/qqmlbinding/WithBindableProperties.h create mode 100644 tests/auto/qml/qqmlbinding/data/restoreBinding5.qml (limited to 'tests/auto/qml/qqmlbinding') diff --git a/tests/auto/qml/qqmlbinding/CMakeLists.txt b/tests/auto/qml/qqmlbinding/CMakeLists.txt index c95d3e6f29..6bf1668970 100644 --- a/tests/auto/qml/qqmlbinding/CMakeLists.txt +++ b/tests/auto/qml/qqmlbinding/CMakeLists.txt @@ -14,6 +14,7 @@ qt_internal_add_test(tst_qqmlbinding SOURCES ../../shared/util.cpp ../../shared/util.h tst_qqmlbinding.cpp + WithBindableProperties.h INCLUDE_DIRECTORIES ../../shared PUBLIC_LIBRARIES @@ -25,6 +26,14 @@ qt_internal_add_test(tst_qqmlbinding TESTDATA ${test_data} ) +set_target_properties(tst_qqmlbinding PROPERTIES + QT_QML_MODULE_URI "test" + QT_QML_MODULE_VERSION 1.0 +) + +qt6_qml_type_registration(tst_qqmlbinding) + + ## Scopes: ##################################################################### diff --git a/tests/auto/qml/qqmlbinding/WithBindableProperties.h b/tests/auto/qml/qqmlbinding/WithBindableProperties.h new file mode 100644 index 0000000000..562b445df7 --- /dev/null +++ b/tests/auto/qml/qqmlbinding/WithBindableProperties.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef WithBindableProperties_H +#define WithBindableProperties_H + +#include +#include + + +class WithBindableProperties : public QObject +{ + Q_OBJECT + QML_ELEMENT + + Q_PROPERTY(int a READ a WRITE setA BINDABLE bindableA) + Q_PROPERTY(int b READ b WRITE setB BINDABLE bindableB) + +public: + QProperty m_a; + QProperty m_b; + int a() {return m_a;} + int b() {return m_b;} + void setA(int val) {m_a = val;} + void setB(int val) {m_b = val;} + QBindable bindableA() {return QBindable(&m_a); } + QBindable bindableB() {return QBindable(&m_b); } +}; + +#endif + diff --git a/tests/auto/qml/qqmlbinding/data/restoreBinding5.qml b/tests/auto/qml/qqmlbinding/data/restoreBinding5.qml new file mode 100644 index 0000000000..75926c3d6e --- /dev/null +++ b/tests/auto/qml/qqmlbinding/data/restoreBinding5.qml @@ -0,0 +1,24 @@ +import QtQuick 2.0 +import test 1 + +Rectangle { + width: 400 + height: 400 + + WithBindableProperties { + id: myItem + objectName: "myItem" + a: 100 - myItem.b + + Binding on a { + when: myItem.b > 50 + value: myItem.b + } + + /*NumberAnimation on y { + loops: Animation.Infinite + to: 100 + duration: 1000 + }*/ + } +} diff --git a/tests/auto/qml/qqmlbinding/tst_qqmlbinding.cpp b/tests/auto/qml/qqmlbinding/tst_qqmlbinding.cpp index afb42453d4..bc2be554a5 100644 --- a/tests/auto/qml/qqmlbinding/tst_qqmlbinding.cpp +++ b/tests/auto/qml/qqmlbinding/tst_qqmlbinding.cpp @@ -31,6 +31,7 @@ #include #include #include "../../shared/util.h" +#include "WithBindableProperties.h" class tst_qqmlbinding : public QQmlDataTest { @@ -42,6 +43,7 @@ private slots: void binding(); void whenAfterValue(); void restoreBinding(); + void restoreBindingBindablePorperty(); void restoreBindingValue(); void restoreBindingVarValue(); void restoreBindingJSValue(); @@ -134,6 +136,34 @@ void tst_qqmlbinding::restoreBinding() QCOMPARE(myItem->x(), qreal(100-49)); } +void tst_qqmlbinding::restoreBindingBindablePorperty() +{ + QQmlEngine engine; + QQmlComponent c(&engine, testFileUrl("restoreBinding5.qml")); + QScopedPointer rect { qobject_cast(c.create()) }; + QVERIFY2(rect, qPrintable(c.errorString())); + + auto *myItem = rect->findChild("myItem"); + QVERIFY(myItem != nullptr); + + myItem->setB(25); + QCOMPARE(myItem->a(), qreal(100-25)); + + myItem->setB(13); + QCOMPARE(myItem->a(), qreal(100-13)); + + //Binding takes effect + myItem->setB(51); + QCOMPARE(myItem->a(), qreal(51)); + + myItem->setB(88); + QCOMPARE(myItem->a(), qreal(88)); + + //original binding restored + myItem->setB(49); + QCOMPARE(myItem->a(), qreal(100-49)); +} + void tst_qqmlbinding::restoreBindingValue() { QQmlEngine engine; -- cgit v1.2.3