Structure array sharing between QML and c++
-
Hi everybody,
I'm coding in QML and c++, I have little experience in QML and data sharing between QML and c++.Now I have a structure like this:
struct dataStruct {
Q_GADGET
Q_PROPERTY(QString dataText0 MEMBER dataText0)
Q_PROPERTY(QString dataText1 MEMBER dataText1)
Q_PROPERTY(QString dataText2 MEMBER dataText2)
Q_PROPERTY(QString dataText3 MEMBER dataText3)
...
Q_PROPERTY(bool data0 MEMBER data0)
Q_PROPERTY(bool data1 MEMBER data1)
Q_PROPERTY(bool data2 MEMBER data2)
Q_PROPERTY(bool data3 MEMBER data3)public:
QString dataText0;
QString dataText1;
QString dataText2;
QString dataText3;... bool data0; bool data1; bool data2; bool data3;
};
Q_DECLARE_METATYPE(dataStruct )And all works well.
Now, I want to change my struct in:
struct channeStruct {
Q_GADGET
Q_PROPERTY(QString dataText MEMBER dataText)
...
Q_PROPERTY(bool data MEMBER data)public:
QString dataText;
...
bool data;
};
Q_DECLARE_METATYPE(channeStruct )struct futureStruct
{
Q_GADGET
???public:
channeStruct ch[n];
};
Q_DECLARE_METATYPE(futureStruct )So i can change my c++ and qml code from
dataStruct .dataText0 = "abc"
...
dataStruct .data0 = falseto
futureStruct .ch[0].dataText = "abc"
...
futureStruct .ch[0].data = falseMy question is:
how can I expose channeStruct ch[n] in futureStruct to QML?Thank in advance for help
-
Ok.
At the end, I found a solution.
I want to share this solution with you.struct channelStr {
Q_GADGET
Q_PROPERTY(QString Text MEMBER Text)
...
Q_PROPERTY(int data MEMBER data)public:
QString Text;
...
int data;
};
Q_DECLARE_METATYPE(channelStr )struct myStructData {
Q_GADGET
Q_PROPERTY(QString exampleMyVersion MEMBER exampleMyVersion )
...
Q_PROPERTY(int exampleMyData MEMBER exampleMyData )public:
channelStr ch[4];
Q_INVOKABLE QVariant getCh( int index ) { return QVariant::fromValue(ch[index]); }QString exampleMyVersion; ... int exampleMyData
};
Q_DECLARE_METATYPE(myStructData )I'm still coding and testing it, but at the moment seems to work well.
Does anyone see any problem in this way?
Thank in advance
CP71