Passing a struct from c++ to qml
I'm writing a control GUI software using QML and C++ to control a hardware and have a question. The GUI has tens of switches, textFields, buttons, etc and I want to control them by mouse click and a configuration file. The fileio was implemented on c++ side because it needs to interface with a hardware via TCP and needs to do some computations.
I have used Q_PROPERTY() to pass some status text back to qml side to display on the GUI and it worked. But using this for all of the controls does not seem right way to go. So, I created a struct which can bundle the controls and tried to pass it to the QML side and got the error below.
QMetaProperty::read: Unable to handle unregistered datatype 'structAeConfigValue' for property 'tcpservice::aeConfigValue'
qrc:/PageAeForm.ui.qml:937: TypeError: Cannot read property 'test' of undefined
Since it complained about having not registered type, I did a google search and added qmlRegisterType function in the main.
Now I am getting multiple errors complaining on 'staticMetaObject' not being a member of the struct type I created.
Here are my questions:
- How can I pass a struct or a bundle of many fields from c++ to QML?
- Is there a better than to do than using QProperty() for this purpose? If there is, what is it?
Thanks for your help.
structAeConfigValuea QObject derivative ? If not, you won't be able to use it in QML the way you describe it.
No, that's fine as it is.
This may help : Best way to access a cpp structure in QML
Thank you all.
It modified the code to use one of the supported data types - QList of QVariantMap.
It seems using QVariantMap works, but not QList of QVariantMap. I guess I am not accessing a item in the list correctly.
Working code with QVariantMap on QML side
Not working code with QList<QVariantMap> on QML side
Is it not right to use index value to access an element of a QList in QML? When I tried to access, I got this error.
TypeError: Cannot read property 'bool' of undefined
Please let me know if you have any clue.
I don't know if it is right way to do it or not, but I was able to get around the problem by using QVariantMap of QJsonArray. Basically, I made a map which has arrays as values.
Thank you all.