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. Structure array sharing between QML and c++
Forum Updated to NodeBB v4.3 + New Features

Structure array sharing between QML and c++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 459 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.
  • C Offline
    C Offline
    CP71
    wrote on 21 Apr 2022, 07:42 last edited by
    #1

    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 = false

    to

    futureStruct .ch[0].dataText = "abc"
    ...
    futureStruct .ch[0].data = false

    My question is:
    how can I expose channeStruct ch[n] in futureStruct to QML?

    Thank in advance for help

    C 1 Reply Last reply 21 Apr 2022, 10:26
    0
    • C CP71
      21 Apr 2022, 07:42

      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 = false

      to

      futureStruct .ch[0].dataText = "abc"
      ...
      futureStruct .ch[0].data = false

      My question is:
      how can I expose channeStruct ch[n] in futureStruct to QML?

      Thank in advance for help

      C Offline
      C Offline
      CP71
      wrote on 21 Apr 2022, 10:26 last edited by
      #2

      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

      1 Reply Last reply
      0

      1/2

      21 Apr 2022, 07:42

      • Login

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