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. How to access C++ struct elements in QML
Forum Updated to NodeBB v4.3 + New Features

How to access C++ struct elements in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 579 Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Praveen.Illa
    wrote on last edited by Praveen.Illa
    #1

    Hi Team,

    How can I access struct elements declared in Qt-C++ from QML.
    Please find sample piece of code and pls let me know what I am doing wrong
    Thank you

    **Sample.h**
    struct Details : public QObject
     {
              QString deviceName;
     }
    class Sample : public QObject
    {
          Q_PROPERTY(QList<Details *> list READ getList CONSTANT)
    }
    
    **Sample.cpp**
    Sample::Sample()
    {
        Details *dd1 = new Details ;
        dd1->setName("cc1");
        m_list.append(dd1);
    }
    QList<DeviceDetails *> Sample::getList()
    {
        return m_list;
    }
    
    **main.cpp**
    Sample sampleObj;
    engine.rootContext()->setContextProperty("qmlSample", &sampleObj);
    
    **main.qml**
        Column {
            anchors.centerIn: parent
            spacing: 10
            Repeater {
                model: qmlSample.list
    
                delegate: Row {
                    spacing: 10
    
                        Text {
                            text: modelData.deviceName
                            font.pixelSize: 15
                        }
                }
            }
        }
    
    
    sierdzioS 1 Reply Last reply
    0
    • P Praveen.Illa

      Hi Team,

      How can I access struct elements declared in Qt-C++ from QML.
      Please find sample piece of code and pls let me know what I am doing wrong
      Thank you

      **Sample.h**
      struct Details : public QObject
       {
                QString deviceName;
       }
      class Sample : public QObject
      {
            Q_PROPERTY(QList<Details *> list READ getList CONSTANT)
      }
      
      **Sample.cpp**
      Sample::Sample()
      {
          Details *dd1 = new Details ;
          dd1->setName("cc1");
          m_list.append(dd1);
      }
      QList<DeviceDetails *> Sample::getList()
      {
          return m_list;
      }
      
      **main.cpp**
      Sample sampleObj;
      engine.rootContext()->setContextProperty("qmlSample", &sampleObj);
      
      **main.qml**
          Column {
              anchors.centerIn: parent
              spacing: 10
              Repeater {
                  model: qmlSample.list
      
                  delegate: Row {
                      spacing: 10
      
                          Text {
                              text: modelData.deviceName
                              font.pixelSize: 15
                          }
                  }
              }
          }
      
      
      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @Praveen-Illa said in How to access C++ struct elements in QML:

      modelData.deviceName

      Just deviceName should be enough.

      I hope you have used Q_OBJECT macro in all these QObject subclasses? And you have initialized parent class properly in constructor?

      Are you getting any error from QML? If not, the problem might be much simpler: your Text, Row, Column elements have no dimensions (width and height).

      (Z(:^

      P 1 Reply Last reply
      0
      • sierdzioS sierdzio

        @Praveen-Illa said in How to access C++ struct elements in QML:

        modelData.deviceName

        Just deviceName should be enough.

        I hope you have used Q_OBJECT macro in all these QObject subclasses? And you have initialized parent class properly in constructor?

        Are you getting any error from QML? If not, the problem might be much simpler: your Text, Row, Column elements have no dimensions (width and height).

        P Offline
        P Offline
        Praveen.Illa
        wrote on last edited by Praveen.Illa
        #3

        @sierdzio Hi, Thank you for your response.
        Yes, I have define Q_OBJECT macro in all subclasses.
        Even though, I am not able to access the struct elements.
        Since, the above sample code provided is pseudo code, so I haven't mentioned all the details. sorry for that.
        I came across this below link from stack overflow but I am not sure wether Qt supports or not support exposing struct elements to QML
        https://stackoverflow.com/questions/7980900/qt-qml-c-qlist-of-structs-as-custom-listview-model#:~:text=QML cannot access "low-level,access these properties in QML.

        B 1 Reply Last reply
        0
        • P Praveen.Illa

          @sierdzio Hi, Thank you for your response.
          Yes, I have define Q_OBJECT macro in all subclasses.
          Even though, I am not able to access the struct elements.
          Since, the above sample code provided is pseudo code, so I haven't mentioned all the details. sorry for that.
          I came across this below link from stack overflow but I am not sure wether Qt supports or not support exposing struct elements to QML
          https://stackoverflow.com/questions/7980900/qt-qml-c-qlist-of-structs-as-custom-listview-model#:~:text=QML cannot access "low-level,access these properties in QML.

          B Offline
          B Offline
          Bob64
          wrote on last edited by
          #4

          @Praveen-Illa in the code you provided in your initial message, you did not show the Q_OBJECT macros. If you are defining them, that means we are not seeing the same code that you are using so it is difficult to see what you might be doing wrong.

          One thing I notice from your code is that you don't define deviceName as a Q_PROPERTY (see the answers in the StackOverflow link you mentioned). Is it the same in your real code?

          Another thing to mention is that if you are just exposing a simple struct, it might be worth looking into using Q_GADGET instead.

          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