aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJędrzej Nowacki <jedrzej.nowacki@qt.io>2018-09-28 12:17:59 +0200
committerJędrzej Nowacki <jedrzej.nowacki@qt.io>2018-10-08 10:02:04 +0000
commit01ed625542f9b4233b8c1a940f78eff7292fc350 (patch)
treec87838d908b8faad2ec2174f157c03fbfb9fe4de
parent5f4b3fa3cade416b088fdc35f583c536269688d7 (diff)
Teach QML that it is possible to treat custom QObjects as QObject
It is always better to treat a QObject child as QObject then a QVariant. Change-Id: I4ac069e85232dd4d1411e325bc5b858bd001f9a6 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
-rw-r--r--src/qml/jsruntime/qv4engine.cpp10
-rw-r--r--tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp8
2 files changed, 15 insertions, 3 deletions
diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp
index f90a693265..3d6a13d794 100644
--- a/src/qml/jsruntime/qv4engine.cpp
+++ b/src/qml/jsruntime/qv4engine.cpp
@@ -1660,9 +1660,13 @@ QV4::ReturnedValue ExecutionEngine::metaTypeToJS(int type, const void *data)
return QV4::Encode::null();
}
QMetaType mt(type);
- if (mt.flags() & QMetaType::IsGadget) {
- Q_ASSERT(mt.metaObject());
- return QV4::QQmlValueTypeWrapper::create(this, QVariant(type, data), mt.metaObject(), type);
+ if (auto metaObject = mt.metaObject()) {
+ auto flags = mt.flags();
+ if (flags & QMetaType::IsGadget) {
+ return QV4::QQmlValueTypeWrapper::create(this, QVariant(type, data), metaObject, type);
+ } else if (flags & QMetaType::PointerToQObject) {
+ return QV4::QObjectWrapper::wrap(this, *reinterpret_cast<QObject* const *>(data));
+ }
}
if (QMetaType::hasRegisteredConverterFunction(type, qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>())) {
auto v = QVariant(type, data);
diff --git a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp
index 5cff44c3cc..6ccfc77c25 100644
--- a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp
+++ b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp
@@ -1720,6 +1720,14 @@ void tst_qqmlvaluetypes::sequences()
for (int i = 0; i < objSet.size(); ++i)
QCOMPARE(value.property(i).property("point").property("x").toInt(), a.point().x());
}
+ {
+ MyTypeObject a, b, c;
+ QSet<MyTypeObject*> container{&a, &b, &c};
+ QJSValue value = engine.toScriptValue(container);
+ QCOMPARE(value.property("length").toInt(), container.size());
+ for (int i = 0; i < container.size(); ++i)
+ QCOMPARE(value.property(i).property("point").property("x").toInt(), a.point().x());
+ }
}
struct StringLessGadget {
Q_GADGET