diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2024-01-19 13:26:32 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2024-01-25 18:35:05 +0100 |
commit | da6680cb2eeea4c845cffac0a0f9d24e30b1625c (patch) | |
tree | 6411f8b6d49bf89fd8dc66232b9a07bc0c74b7a8 /tests/auto/qml/qmlcppcodegen | |
parent | cc761e9c5a1c5249f4760c8e5e57755a3fe042ca (diff) |
QmlCompiler: Implement resetting of properties
We piggy-back on the mechanism used to handle shadowable properties and
pass the value as QVariant. QVariant can hold undefined and the lookup
functions know how to handle it.
Pick-to: 6.7 6.6 6.5 6.2
Fixes: QTBUG-120512
Change-Id: I9bca4940256c82bdcf5540b956600eb420be363e
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/qml/qmlcppcodegen')
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/resettable.h | 39 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/resettable.qml | 15 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp | 23 |
4 files changed, 79 insertions, 0 deletions
diff --git a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt index c82cacd089..67d8962983 100644 --- a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt +++ b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt @@ -17,6 +17,7 @@ set(cpp_sources multiforeign.h objectwithmethod.h person.cpp person.h + resettable.h sequenceToIterable.h sequencetypeexample.cpp sequencetypeexample.h state.h @@ -219,6 +220,7 @@ set(qml_files registerPropagation.qml registerelimination.qml renameAdjust.qml + resettable.qml returnAfterReject.qml revisions.qml scopeIdLookup.qml diff --git a/tests/auto/qml/qmlcppcodegen/data/resettable.h b/tests/auto/qml/qmlcppcodegen/data/resettable.h new file mode 100644 index 0000000000..129ef36ef4 --- /dev/null +++ b/tests/auto/qml/qmlcppcodegen/data/resettable.h @@ -0,0 +1,39 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef RESETTABLE_H +#define RESETTABLE_H + +#include <QtCore/qobject.h> +#include <QtQml/qqml.h> + +class ResettableProperty : public QObject +{ + Q_OBJECT + QML_NAMED_ELEMENT(Resettable) + Q_PROPERTY(qreal value READ value WRITE setValue RESET resetValue NOTIFY valueChanged FINAL) + Q_PROPERTY(qreal shadowable READ shadowable CONSTANT) + +public: + explicit ResettableProperty(QObject *parent = nullptr) : QObject(parent) {} + qreal value() const { return m_value; } + qreal shadowable() const { return 25; } + +public slots: + void resetValue() { setValue(0); } + void setValue(qreal value) + { + if (m_value == value) + return; + m_value = value; + emit valueChanged(); + } + +signals: + void valueChanged(); + +private: + qreal m_value = 0; +}; + +#endif // RESETTABLE_H diff --git a/tests/auto/qml/qmlcppcodegen/data/resettable.qml b/tests/auto/qml/qmlcppcodegen/data/resettable.qml new file mode 100644 index 0000000000..59fa31645e --- /dev/null +++ b/tests/auto/qml/qmlcppcodegen/data/resettable.qml @@ -0,0 +1,15 @@ +pragma Strict +import QtQml +import TestTypes + +Resettable { + id: self + value: 999 + + property Resettable shadowing: Resettable { + property var shadowable: undefined + } + + function doReset() { self.value = undefined } + function doReset2() { self.value = shadowing.shadowable } +} diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp index f732998647..548ad20c2b 100644 --- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp +++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp @@ -8,6 +8,7 @@ #include <data/enumproblems.h> #include <data/getOptionalLookup.h> #include <data/objectwithmethod.h> +#include <data/resettable.h> #include <data/weathermoduleurl.h> #include <data/withlength.h> @@ -186,6 +187,7 @@ private slots: void registerElimination(); void registerPropagation(); void renameAdjust(); + void resettableProperty(); void returnAfterReject(); void revisions(); void scopeIdLookup(); @@ -3837,6 +3839,27 @@ void tst_QmlCppCodegen::renameAdjust() QVERIFY(o); } +void tst_QmlCppCodegen::resettableProperty() +{ + QQmlEngine engine; + QQmlComponent c(&engine, QUrl(u"qrc:/qt/qml/TestTypes/resettable.qml"_s)); + QVERIFY2(c.isReady(), qPrintable(c.errorString())); + QScopedPointer<QObject> o(c.create()); + QVERIFY(o); + + ResettableProperty *resettable = qobject_cast<ResettableProperty *>(o.data()); + QVERIFY(resettable); + + QCOMPARE(resettable->value(), 999); + QMetaObject::invokeMethod(resettable, "doReset"); + QCOMPARE(resettable->value(), 0); + + resettable->setValue(82); + QCOMPARE(resettable->value(), 82); + QMetaObject::invokeMethod(resettable, "doReset2"); + QCOMPARE(resettable->value(), 0); +} + void tst_QmlCppCodegen::returnAfterReject() { QQmlEngine engine; |