Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Passing a struct from c++ to qml
Forum Update on Monday, May 27th 2025

Passing a struct from c++ to qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 5.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gongdori
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • P Offline
        P Offline
        pra7
        wrote on last edited by
        #3

        This may help : Best way to access a cpp structure in QML

        1 Reply Last reply
        1
        • G Offline
          G Offline
          gongdori
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gongdori
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved