How to declare metatypes in QML
-
Dear
I'm facing a problem with metatypes in QML. I have defined a custom metatype in c++:
class Topic { Q_GADGET Q_PROPERTY(bool valid ...) } Q_DECLARE_METATYPE(Topic)
In main, I have included this:
qRegisterMetaType<Topic>("Topic");
When propagating these elements from c++ to QML, everything is working. I can access the property.
I however have a scenario where I want to declare such an object in qml and transmit it to the c++. For that, I want to declare a variable of type "Topic" and use it in a Q_INVOKABLE function. I however can't find a way to declare such a "Topic" object in qml.
How do I do this?
-
Dear
I'm facing a problem with metatypes in QML. I have defined a custom metatype in c++:
class Topic { Q_GADGET Q_PROPERTY(bool valid ...) } Q_DECLARE_METATYPE(Topic)
In main, I have included this:
qRegisterMetaType<Topic>("Topic");
When propagating these elements from c++ to QML, everything is working. I can access the property.
I however have a scenario where I want to declare such an object in qml and transmit it to the c++. For that, I want to declare a variable of type "Topic" and use it in a Q_INVOKABLE function. I however can't find a way to declare such a "Topic" object in qml.
How do I do this?
@Pieter-Cardoen if I understand you correctly, I don't think it will be possible to do what you want to do. The C++ can only ever know about types declared in C++ at compile time.
-
@Pieter-Cardoen if I understand you correctly, I don't think it will be possible to do what you want to do. The C++ can only ever know about types declared in C++ at compile time.
Dear @Bob64
The topic goes about using the metatype in qml/javascript. I want to do this kind of things:
MouseArea{ onClicked: { Topic t // Declare variable -> this is what I want to do t.valid = false // use t in a Q_INVOKABLE function } }
-
Sadly this is not possible for the moment : https://bugreports.qt.io/browse/QTBUG-72223
What you can do instead is have a c++ factory Q_INVOKABLE function returning a gadget:
MouseArea{ onClicked: { let t = cppBackend.createTopic(); t.valid = false // use t in a Q_INVOKABLE function } }