Unsolved Instanting Q_GADGET
-
Hi everyone, http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#value-types describes that Q_GADGET objects are available in QML but there is no example of how to use them in QML (there's only a C++ snippet).
Could you tell me how to create an instance of the Actor class defined in the documentation in QML/Javascript?
Thanks in advance -
4 years later... but I have stumbled upon this topic and I have an answer, so maybe it will help somebody.
@VRonin said in Instanting Q_GADGET:
Could you tell me how to create an instance of the Actor class defined in the documentation in QML/Javascript?
You cannot instantiate Q_GADGETs in QML. But you can pass them from C++ into QML in 2 ways:
- via a property of another Q_OBJECT (this way you can even pass a default-constructed gadget, which is practically 100% the same as if you instantiated it in QML)
- via signal
For example:
class Gadget { Q_GADGET Q_PROPERTY(QString data MEMBER data) public: QString data; } Q_DECLARE_METATYPE(Gadget) class Object { Q_OBJECT Q_PROPERTY(Gadget gadget READ gadget) public: Gadget gadget() { Gadget result; return result; } } // QML: Object { id: myObject Component.onCompleted: console.log("Gadget data is:", gadget.data) }