aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-06-02 08:42:51 +0200
committerUlf Hermann <ulf.hermann@qt.io>2021-06-02 10:18:03 +0200
commit28c4d536e2bbf65d85c89efe1b64fc4e714e02bc (patch)
tree474126a7bbefc8a1cc0cd73d41288718da497e9c /tests
parent19b6f9591257b3a07bf1479bcc2d676e57506447 (diff)
Allow value type conversion in metaTypeFromJS
We implicitly do the same when calling toVariant(). Change-Id: I288326125d88bc658dcaf12d3ee623e0e529bb69 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp
index 9c732e4ce3..c062b9cd56 100644
--- a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp
+++ b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp
@@ -101,6 +101,7 @@ private slots:
void nonValueTypes();
void char16Type();
void writeBackOnFunctionCall();
+ void valueTypeConversions();
private:
QQmlEngine engine;
@@ -1928,6 +1929,53 @@ void tst_qqmlvaluetypes::writeBackOnFunctionCall()
QCOMPARE(s->writeCount, 2);
}
+struct TypeB;
+struct TypeA
+{
+ Q_GADGET
+
+public:
+ TypeA() = default;
+ TypeA(const TypeB &other);
+ TypeA &operator=(const TypeB &other);
+
+ int a = 4;
+};
+
+struct TypeB
+{
+ Q_GADGET
+
+public:
+ TypeB() = default;
+ TypeB(const TypeA &other) : b(other.a) {}
+ TypeB &operator=(const TypeA &other) { b = other.a; return *this; }
+
+ int b = 5;
+};
+
+TypeA::TypeA(const TypeB &other) : a(other.b) {}
+TypeA &TypeA::operator=(const TypeB &other) { a = other.b; return *this; }
+
+void tst_qqmlvaluetypes::valueTypeConversions()
+{
+ QMetaType::registerConverter<TypeA, TypeB>();
+ QMetaType::registerConverter<TypeB, TypeA>();
+
+ TypeA a;
+ TypeB b;
+
+ QJSEngine engine;
+ QJSValue jsA = engine.toScriptValue(a);
+ QJSValue jsB = engine.toScriptValue(b);
+
+ TypeA resultA = engine.fromScriptValue<TypeA>(jsB);
+ TypeB resultB = engine.fromScriptValue<TypeB>(jsA);
+
+ QCOMPARE(resultA.a, b.b);
+ QCOMPARE(resultB.b, a.a);
+}
+
#undef CHECK_TYPE_IS_NOT_VALUETYPE
QTEST_MAIN(tst_qqmlvaluetypes)