From 2b5fb185627f8adfb6c5b3d62990a58429bf4ea7 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Thu, 20 Nov 2014 10:42:34 +0100 Subject: Enable gadget wrapping for custom value types [ChangeLog][QtQml] Custom C++ value types annotated with Q_GADGET are now fully accessible in the QML and QJSEngine JavaScript environment. QJSEngine::toScriptValue can be used for injection and fromScriptValue to extraction. The QML "built-in" gadget wrappers for QPoint and the gui types are not exposed this way, toScriptValue(point) will still return an opaque QVariant wrapper. We could expose the core types right away, but then we would be lacking an API to enable use of the Gui types that are registered in QtQuick. It would be better to make the core types in qtbase gadgets and thus enable them without the need for hooks and init functions to be called by the user. Task-number: QTBUG-29769 Change-Id: I8179cd599bdc1209ff61cfdbdda419cb400296bb Reviewed-by: Lars Knoll --- src/qml/qml/v8/qv8engine.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/qml/qml/v8/qv8engine.cpp') diff --git a/src/qml/qml/v8/qv8engine.cpp b/src/qml/qml/v8/qv8engine.cpp index cf66c5c2f3..0a309c2775 100644 --- a/src/qml/qml/v8/qv8engine.cpp +++ b/src/qml/qml/v8/qv8engine.cpp @@ -700,10 +700,14 @@ QV4::ReturnedValue QV8Engine::metaTypeToJS(int type, const void *data) QByteArray typeName = QMetaType::typeName(type); if (typeName.endsWith('*') && !*reinterpret_cast(data)) { return QV4::Encode::null(); - } else { - // Fall back to wrapping in a QVariant. - return QV4::Encode(m_v4Engine->newVariantObject(QVariant(type, data))); } + QMetaType mt(type); + if (mt.flags() & QMetaType::IsGadget) { + Q_ASSERT(mt.metaObject()); + return QV4::QQmlValueTypeWrapper::create(m_v4Engine, QVariant(type, data), mt.metaObject(), type); + } + // Fall back to wrapping in a QVariant. + return QV4::Encode(m_v4Engine->newVariantObject(QVariant(type, data))); } } Q_UNREACHABLE(); @@ -835,6 +839,14 @@ bool QV8Engine::metaTypeFromJS(const QV4::ValueRef value, int type, void *data) ; } + { + QV4::Scoped vtw(scope, value); + if (vtw && vtw->d()->metaType == type) { + vtw->toGadget(data); + return true; + } + } + #if 0 if (isQtVariant(value)) { const QVariant &var = variantValue(value); -- cgit v1.2.3