summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2023-05-09 16:59:24 +0200
committerFabian Kosmale <fabian.kosmale@qt.io>2023-05-12 15:06:42 +0200
commit13fc038d8d4baa9e199fbd5a7e4c08650a88172e (patch)
treef0f01ea5594bd3c82a9faa97645c8dbc37b73476
parentf3b1d6135222e9ce41845a00831a3eb24cd36449 (diff)
downloadqtdeclarative-13fc038d8d4baa9e199fbd5a7e4c08650a88172e.tar.gz
Fix reset support for value types
Our value type refactoring in 6.5 broke reset support for value types when that write happens through virtualPut. Fix that by checking whether the value we're assinging is undefined before attempting any conversion, and reset the gadget in that case (if it is actually resettable). Task-number: QTBUG-113473 Pick-to: 6.5 Change-Id: Ifaa2d045f718fc3cb2d5e75b3626b41175ac3a3b Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> Reviewed-by: Semih Yavuz <semih.yavuz@qt.io>
-rw-r--r--src/qml/qml/qqmlvaluetypewrapper.cpp6
-rw-r--r--tests/auto/qml/qqmlecmascript/data/resetGadget.qml9
-rw-r--r--tests/auto/qml/qqmlecmascript/testtypes.cpp1
-rw-r--r--tests/auto/qml/qqmlecmascript/testtypes.h35
-rw-r--r--tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp16
5 files changed, 67 insertions, 0 deletions
diff --git a/src/qml/qml/qqmlvaluetypewrapper.cpp b/src/qml/qml/qqmlvaluetypewrapper.cpp
index d203f07e87..3f2ff8ef8e 100644
--- a/src/qml/qml/qqmlvaluetypewrapper.cpp
+++ b/src/qml/qml/qqmlvaluetypewrapper.cpp
@@ -835,6 +835,12 @@ bool QQmlValueTypeWrapper::virtualPut(Managed *m, PropertyKey id, const Value &v
QMetaProperty property = metaObject->property(pd.coreIndex());
Q_ASSERT(property.isValid());
+ if (value.isUndefined() && pd.isResettable()) {
+ property.resetOnGadget(reinterpret_cast<QObject *>(r->d()->gadgetPtr()));
+ if (heapObject)
+ r->d()->writeBack(pd.coreIndex());
+ return true;
+ }
QVariant v = QV4::ExecutionEngine::toVariant(value, property.metaType());
diff --git a/tests/auto/qml/qqmlecmascript/data/resetGadget.qml b/tests/auto/qml/qqmlecmascript/data/resetGadget.qml
new file mode 100644
index 0000000000..2bc196da34
--- /dev/null
+++ b/tests/auto/qml/qqmlecmascript/data/resetGadget.qml
@@ -0,0 +1,9 @@
+import Qt.test
+
+ResettableGadgetHolder {
+ id: root
+ property bool trigger: false
+ onTriggerChanged: {
+ root.g.value = undefined
+ }
+}
diff --git a/tests/auto/qml/qqmlecmascript/testtypes.cpp b/tests/auto/qml/qqmlecmascript/testtypes.cpp
index 40f5e5cf5c..d7a585de1a 100644
--- a/tests/auto/qml/qqmlecmascript/testtypes.cpp
+++ b/tests/auto/qml/qqmlecmascript/testtypes.cpp
@@ -542,6 +542,7 @@ void registerTypes()
qmlRegisterType<Receiver>("Qt.test", 1,0, "Receiver");
qmlRegisterType<Sender>("Qt.test", 1,0, "Sender");
qmlRegisterTypesAndRevisions<ReadOnlyBindable>("Qt.test", 1);
+ qmlRegisterTypesAndRevisions<ResettableGadgetHolder>("Qt.test", 1);
}
#include "testtypes.moc"
diff --git a/tests/auto/qml/qqmlecmascript/testtypes.h b/tests/auto/qml/qqmlecmascript/testtypes.h
index ff9dda36d1..bc4d97237c 100644
--- a/tests/auto/qml/qqmlecmascript/testtypes.h
+++ b/tests/auto/qml/qqmlecmascript/testtypes.h
@@ -2000,6 +2000,41 @@ public:
QBindable<int> bindableX() const { return &_xProp; }
};
+class ResettableGadget
+{
+ Q_GADGET
+ Q_PROPERTY(qreal value READ value WRITE setValue RESET resetValue)
+
+ qreal m_value = 0;
+
+public:
+ qreal value() const { return m_value; }
+ void setValue(qreal val) { m_value = val; }
+ void resetValue() { m_value = 42; }
+};
+
+class ResettableGadgetHolder : public QObject {
+ Q_OBJECT
+ QML_ELEMENT
+
+ Q_PROPERTY(ResettableGadget g READ g WRITE setG NOTIFY gChanged)
+ ResettableGadget m_g;
+
+signals:
+ void gChanged();
+
+public:
+ ResettableGadget g() const { return m_g; }
+ void setG(ResettableGadget newG)
+ {
+ if (m_g.value() == newG.value())
+ return;
+ m_g = newG;
+ Q_EMIT gChanged();
+ }
+};
+
+
void registerTypes();
#endif // TESTTYPES_H
diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
index edb1e9ba80..98f81c4648 100644
--- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
+++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
@@ -416,6 +416,8 @@ private slots:
void doNotCrashOnReadOnlyBindable();
+ void resetGadet();
+
private:
// static void propertyVarWeakRefCallback(v8::Persistent<v8::Value> object, void* parameter);
static void verifyContextLifetime(const QQmlRefPointer<QQmlContextData> &ctxt);
@@ -10373,6 +10375,20 @@ void tst_qqmlecmascript::doNotCrashOnReadOnlyBindable()
QCOMPARE(o->property("x").toInt(), 7);
}
+void tst_qqmlecmascript::resetGadet()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, testFileUrl("resetGadget.qml"));
+ QVERIFY2(c.isReady(), qPrintable(c.errorString()));
+ QScopedPointer<QObject> o(c.create());
+ QVERIFY(o);
+ auto resettableGadgetHolder = qobject_cast<ResettableGadgetHolder *>(o.get());
+ QVERIFY(resettableGadgetHolder);
+ QCOMPARE(resettableGadgetHolder->g().value(), 0);
+ resettableGadgetHolder->setProperty("trigger", QVariant::fromValue(true));
+ QCOMPARE(resettableGadgetHolder->g().value(), 42);
+}
+
QTEST_MAIN(tst_qqmlecmascript)
#include "tst_qqmlecmascript.moc"