C++ return to QML
-
Can anyone point / help me to a good example / tutorial on using QVariant to construct an object that can be returned to QML ?
@SPlatten said in C++ return to QML:
Can anyone point / help me to a good example / tutorial on using QVariant to construct an object that can be returned to QML ?
For your type to be accessible in QML it is either a primitive, a gadget (a.k.a. a value type) or a
QObjectderived class. No other options are allowed. If it's a primitive - it's directly accessible. If it's a gadget then it must qualify for a metatype and has to have theQ_GADGETmacro, (usually) have it registered withqRegisterMetatypeif it's a parameter for signal/slot connections, and most importantly be registered with the QML engine withQML_VALUE_TYPE.
https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.htmlQObjectderived types simply have theQ_OBJECTmacro and theQML_ELEMENTannotation.Either way you should declare the available properties with
Q_PROPERTY. -
I have a C++ API function that looks up a particular string to find an object, it returns a reference to the QML. What can I return to the QML if the reference doesn't correspond to any found object ?
I believe I've solved the issue, I've added a local static to the function that is called by the QML:
staledata_t& staledata_t::isRegistered(QString strUDT_) { staledata_t* pobjData(staledata_t::pFindTag(strUDT_)); static staledata_t invalid; if ( pobjData != nullptr ) { return *pobjData; } return invalid; }The invalid variable will only be valid if it has all the required parameters set-up. That works nicely, the next issue, is how can I return an object to the QML that it can interpret? Is JSON an option ?
-
I believe I've solved the issue, I've added a local static to the function that is called by the QML:
staledata_t& staledata_t::isRegistered(QString strUDT_) { staledata_t* pobjData(staledata_t::pFindTag(strUDT_)); static staledata_t invalid; if ( pobjData != nullptr ) { return *pobjData; } return invalid; }The invalid variable will only be valid if it has all the required parameters set-up. That works nicely, the next issue, is how can I return an object to the QML that it can interpret? Is JSON an option ?
-
@SPlatten if you want to pass the object back to QML to do something, why can not you call the object inside staledata_t to do what the object does inside QML?
Also isRegistered() has to be static?
@JoeCFD , thank you, no isRegistered isn't static, prototype:
Q_INVOKABLE staledata_t& isRegistered(QString strUDT_);I think I can create a custom type using QVariant, will look into this further. BTW I said pass not parse, I don't need to manage the passed back object in C++, just return the found data.
-
@JoeCFD , thank you, no isRegistered isn't static, prototype:
Q_INVOKABLE staledata_t& isRegistered(QString strUDT_);I think I can create a custom type using QVariant, will look into this further. BTW I said pass not parse, I don't need to manage the passed back object in C++, just return the found data.
-
Can anyone point / help me to a good example / tutorial on using QVariant to construct an object that can be returned to QML ?
@SPlatten said in C++ return to QML:
Can anyone point / help me to a good example / tutorial on using QVariant to construct an object that can be returned to QML ?
For your type to be accessible in QML it is either a primitive, a gadget (a.k.a. a value type) or a
QObjectderived class. No other options are allowed. If it's a primitive - it's directly accessible. If it's a gadget then it must qualify for a metatype and has to have theQ_GADGETmacro, (usually) have it registered withqRegisterMetatypeif it's a parameter for signal/slot connections, and most importantly be registered with the QML engine withQML_VALUE_TYPE.
https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.htmlQObjectderived types simply have theQ_OBJECTmacro and theQML_ELEMENTannotation.Either way you should declare the available properties with
Q_PROPERTY.