Exposing a user defined class / structure to qt meta object system and accessing the same in qml
-
Hi all,
i was trying something like this..i had defined my own class in cpp which is as below.class myclass{ public : int k;int m; QString str; };
and my code has one more class which has a function ' retval' which will return an object of type myclass
class reduntclass : public QObject { Q_OBJECT public : .................... ................... myclass retval() { ............... .............. return myclass_object; }//ret val ends here .................... ................... .................... ................... };//reduntclass class ends here
and i have exposed "reduntclass" object to qml (with the help of context->setContextProperty("reduntclass", &(reduntclassobj));).also iam able to access reduntclass object from qml.problem is whenever i press a key , qml will call retval function as below
onclicked { ........... var k = reduntclass.retval() ........... }
but qml is not able identify the returned value ,and it gives the below errro message
Error: Unknown method return type: myclass
how i can solve this? i think qml is not able to identify myclass type,so i guess i need to expose my 'myclass' class to qt meta object system? -
QML only understands QObjects. Your
myclass
should be a QObject, or at the very least a Q_GADGET. -
i tried the below code . in my .hpp file i have one structure and one class
struct MyStruct { Q_GADGET int intval; QString str1; QString str2; }; class MyClass:public QObject { Q_OBJECT public: MyClass(QObject *parent = nullptr){ qDebug() <<"object created "; } MyStruct strObj; ~MyClass() { } MyStruct strObj; Q_INVOKABLE MyStruct getMyStruct() const { return strObj; } };
in main.cpp i have below piece of code
MyClass classObj; context->setContextProperty("classObj",&classObj);
on qml
onClicked: { var k = classObj.getMyStruct() }
but still getting below error message,what is wrong with the code?? please help!!!
Error: Unknown method return type: MyStruct
-
i tried the below code . in my .hpp file i have one structure and one class
struct MyStruct { Q_GADGET int intval; QString str1; QString str2; }; class MyClass:public QObject { Q_OBJECT public: MyClass(QObject *parent = nullptr){ qDebug() <<"object created "; } MyStruct strObj; ~MyClass() { } MyStruct strObj; Q_INVOKABLE MyStruct getMyStruct() const { return strObj; } };
in main.cpp i have below piece of code
MyClass classObj; context->setContextProperty("classObj",&classObj);
on qml
onClicked: { var k = classObj.getMyStruct() }
but still getting below error message,what is wrong with the code?? please help!!!
Error: Unknown method return type: MyStruct
This post is deleted! -
I'm not sure if Q_GADGET will work (QObject will for sure). Things to try:
- return a pointer to your gadget, not a copy
- declare pointer to your gadget as Qt meta type, register it with QML
-
1- your struct or simple class must have
Q_GADGET
as minimum
2- you should declare properties in order to access from qml
2- you must declare your struct/class byQ_DECLARE_METATYPE()
3- you must register it usingqRegisterMetaType<>()
somewhere before loading qml file by engine such asmain.cpp
so you have something like this:
struct MyStruct { Q_GADGET Q_PROPERTY(QString str1 MEMBER m_str1) public: QString m_str1; }; Q_DECLARE_METATYPE(MyStruct)
main.cpp
//... qRegisterMetaType<MyStruct>(); //... engine.load(url); //...
NOTE: be careful, is seems that nested classes/struct not supported by Qt meta
-
You can't have a
Q_GADGET
and aQ_OBJECT
in the same header file,moc
is not smart enough to process that.