Qt 5.5 (beta): Custom value types in QML
-
One of the new features of Qt5.5 is the ability to expose custom value types to QML from C++, supposedly similar to Qt.vector3d, Qt.point, etc.
Is there any documentation or example available that explains how to do this? E.g. how do I write such a value type in C++ and how do I expose it to QML?
-
Have a look at https://doc-snapshots.qt.io/qt5-dev/qobject.html#Q_GADGET
You can simply write your plain struct just by adding the macro.
struct MyStruct { QPROPERTY(QString name MEMBER m_name) Q_GADGET; QString m_name; }
Than you can convert it to a QJSValue by simply calling this function: https://doc-snapshots.qt.io/qt5-dev/qjsengine.html#toScriptValue
MyStruct instance { "the name" }; QJSValue value = engine.toScriptValue(instance);
Or you may just return it in a already registered QObject via Q_INVOKABLE or as QPROPERTY.
-
Thanks for your response richardo. That looks remarkably simple. I will experiment with it later.