Passing a struct from c++ to qml



  • Hi all,

    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.
    qmlRegisterType<structAeConfigValue>("contrl_struct",1,0,"structAeConfigValue");

    Now I am getting multiple errors complaining on 'staticMetaObject' not being a member of the struct type I created.

    Here are my questions:

    1. How can I pass a struct or a bundle of many fields from c++ to QML?
    2. Is there a better than to do than using QProperty() for this purpose? If there is, what is it?
      Thanks for your help.

    Gongdori


  • Lifetime Qt Champion

    Hi,

    1. Is structAeConfigValue a QObject derivative ? If not, you won't be able to use it in QML the way you describe it.

    2. No, that's fine as it is.





  • 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

    checked: TcpService.testVm["bool"]
    

    Not working code with QList<QVariantMap> on QML side

    checked: TcpService.aeConfigValue[0]["bool"]
    

    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.
    Thanks,

    Gongdori



  • 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.

    Gongdori


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.