Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. Passing data structures from QML to C++

Passing data structures from QML to C++

Scheduled Pinned Locked Moved Solved Brainstorm
10 Posts 3 Posters 2.3k 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 12 Jan 2023, 17:51 last edited by
    #1

    Hi all -

    I'm posting this in Brainstorming because I'm not sure whether it's a C++ question or a QML question, or somewhere in between.

    I have a C++ class that maintains a QList of a struct I've defined. I need to add a (populated) element to this list from QML. How is this typically accomplished? Should I try to make my QML "struct aware" (something I don't know how to do, or am I better off updating the C++ class one element at a time?

    I hope this makes sense. Here's what my struct looks like (currently defined outside of any class):

    struct Activity {
        Q_GADGET
        Q_PROPERTY(QString name MEMBER m_name)
        Q_PROPERTY(QList<QString> equipment MEMBER m_equipment)
        Q_PROPERTY(bool recommended MEMBER m_recommended)
    public:
        QString m_name;
        QList<QString> m_equipment;
        bool m_recommended;
    };
    Q_DECLARE_METATYPE(Activity)
    
    1 Reply Last reply
    0
    • M mzimmers
      13 Jan 2023, 00:48

      When I try to access this struct in QML, I get an error. Here's my attempt to create an Activity instance:

      Activity {}
      

      At run-time, I get this error: Activity is not a type.

      I also have a method in my C++ class:

      Q_INVOKABLE void appendItem(Activity item) { m_list->appendItem(item); }
      

      which I try to use like so:

      activityModel.appendItem("")
      

      which causes the run-time error:
      Could not convert argument 0 at expression for onClicked: TypeError: Passing incompatible arguments to C++ functions from JavaScript is not allowed.
      I tried this and got the same error as above:

      activityModel.appendItem({name: "from", equipment: ["QML", "YAY"], recommended: false})
      

      So...I'm not sure where to go from here. I suppose I could declare an Activity object in my class, that would be used only for the QML interface, but that sounds like a hack. Any other ideas on this?

      Thanks...

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 14 Jan 2023, 14:21 last edited by
      #9

      @mzimmers said in Passing data structures from QML to C++:

      Here's my attempt to create an Activity instance:

      Activity {}
      

      Use a helper C++ function that returns a default Activity object: https://stackoverflow.com/questions/42797359/how-to-create-new-instance-of-a-q-gadget-struct-in-qml

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      M 1 Reply Last reply 14 Jan 2023, 14:54
      1
      • M Offline
        M Offline
        mzimmers
        wrote on 13 Jan 2023, 00:48 last edited by mzimmers
        #2

        When I try to access this struct in QML, I get an error. Here's my attempt to create an Activity instance:

        Activity {}
        

        At run-time, I get this error: Activity is not a type.

        I also have a method in my C++ class:

        Q_INVOKABLE void appendItem(Activity item) { m_list->appendItem(item); }
        

        which I try to use like so:

        activityModel.appendItem("")
        

        which causes the run-time error:
        Could not convert argument 0 at expression for onClicked: TypeError: Passing incompatible arguments to C++ functions from JavaScript is not allowed.
        I tried this and got the same error as above:

        activityModel.appendItem({name: "from", equipment: ["QML", "YAY"], recommended: false})
        

        So...I'm not sure where to go from here. I suppose I could declare an Activity object in my class, that would be used only for the QML interface, but that sounds like a hack. Any other ideas on this?

        Thanks...

        J 1 Reply Last reply 14 Jan 2023, 14:21
        0
        • J Offline
          J Offline
          JoeCFD
          wrote on 13 Jan 2023, 15:08 last edited by
          #3

          Struct is a class in C++ and treat it as a class.
          https://stackoverflow.com/questions/45650277/best-way-to-access-a-cpp-structure-in-qml

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mzimmers
            wrote on 13 Jan 2023, 16:02 last edited by
            #4

            I tried that:

            class Activity { //: public QObject {
            //    Q_GADGET
            //    Q_OBJECT
            //    Q_PROPERTY(QString name MEMBER m_name)
            //    Q_PROPERTY(QList<QString> equipment MEMBER m_equipment)
            //    Q_PROPERTY(bool recommended MEMBER m_recommended)
            public:
                QString m_name;
                QList<QString> m_equipment;
                bool m_recommended;
            };
            

            and I've registered Activity in main.cpp:

                qRegisterMetaType<Activity>("Activity");
            

            but when I try to use it in my QML file:

            import ActivityList // where Activity is defined
            Activity {}
            

            I get an error: "Activity is not a type."

            J 1 Reply Last reply 13 Jan 2023, 16:17
            0
            • M mzimmers
              13 Jan 2023, 16:02

              I tried that:

              class Activity { //: public QObject {
              //    Q_GADGET
              //    Q_OBJECT
              //    Q_PROPERTY(QString name MEMBER m_name)
              //    Q_PROPERTY(QList<QString> equipment MEMBER m_equipment)
              //    Q_PROPERTY(bool recommended MEMBER m_recommended)
              public:
                  QString m_name;
                  QList<QString> m_equipment;
                  bool m_recommended;
              };
              

              and I've registered Activity in main.cpp:

                  qRegisterMetaType<Activity>("Activity");
              

              but when I try to use it in my QML file:

              import ActivityList // where Activity is defined
              Activity {}
              

              I get an error: "Activity is not a type."

              J Offline
              J Offline
              JoeCFD
              wrote on 13 Jan 2023, 16:17 last edited by
              #5

              @mzimmers you need Q_OBJECT in your class.
              qmlRegisterType may be good enough.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mzimmers
                wrote on 13 Jan 2023, 16:19 last edited by
                #6

                But if I base my class off of Q_OBJECT, I lose the copy constructor and assignment operator. Do I need to create those for my Activity class?

                J 1 Reply Last reply 13 Jan 2023, 16:21
                0
                • M mzimmers
                  13 Jan 2023, 16:19

                  But if I base my class off of Q_OBJECT, I lose the copy constructor and assignment operator. Do I need to create those for my Activity class?

                  J Offline
                  J Offline
                  JoeCFD
                  wrote on 13 Jan 2023, 16:21 last edited by
                  #7

                  @mzimmers Why do you lose them?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mzimmers
                    wrote on 13 Jan 2023, 16:23 last edited by
                    #8

                    https://stackoverflow.com/questions/19092513/copy-constructor-of-derived-qt-class

                    1 Reply Last reply
                    0
                    • M mzimmers
                      13 Jan 2023, 00:48

                      When I try to access this struct in QML, I get an error. Here's my attempt to create an Activity instance:

                      Activity {}
                      

                      At run-time, I get this error: Activity is not a type.

                      I also have a method in my C++ class:

                      Q_INVOKABLE void appendItem(Activity item) { m_list->appendItem(item); }
                      

                      which I try to use like so:

                      activityModel.appendItem("")
                      

                      which causes the run-time error:
                      Could not convert argument 0 at expression for onClicked: TypeError: Passing incompatible arguments to C++ functions from JavaScript is not allowed.
                      I tried this and got the same error as above:

                      activityModel.appendItem({name: "from", equipment: ["QML", "YAY"], recommended: false})
                      

                      So...I'm not sure where to go from here. I suppose I could declare an Activity object in my class, that would be used only for the QML interface, but that sounds like a hack. Any other ideas on this?

                      Thanks...

                      J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 14 Jan 2023, 14:21 last edited by
                      #9

                      @mzimmers said in Passing data structures from QML to C++:

                      Here's my attempt to create an Activity instance:

                      Activity {}
                      

                      Use a helper C++ function that returns a default Activity object: https://stackoverflow.com/questions/42797359/how-to-create-new-instance-of-a-q-gadget-struct-in-qml

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      M 1 Reply Last reply 14 Jan 2023, 14:54
                      1
                      • J Offline
                        J Offline
                        JKSH
                        Moderators
                        wrote on 16 Jan 2023, 14:14 last edited by
                        #10

                        (Further questions forked to https://forum.qt.io/topic/142227/update-c-list-model-from-qml )

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        0

                        9/10

                        14 Jan 2023, 14:21

                        • Login

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