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. What is the best practice for passing data from C++ to QML?

What is the best practice for passing data from C++ to QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 234 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.
  • P Offline
    P Offline
    peter.thompson
    wrote on 26 Mar 2025, 19:03 last edited by peter.thompson
    #1

    I have an existing Qt widgets application that I would like to start transitioning to QML. The application has some C++ data with a complex structure. It can't be forced into any of the Qt models that I am aware of (list, table, or tree). However, it does exist in QObject-derived classes, and I can add the appropriate macros to these classes to make them workable in QML (QML_ELEMENT, Q_PROPERTY, Q_INVOKABLE, etc.).

    What would be the best practice for getting this data from C++ to QML? I will probably be replacing the application with QML one piece at a time, using multiple QQuickWidgets to embed the QML in the widgets app. So I'm curious about best practice in two scenarios:

    • An application that has bits and pieces of QML
    • A pure QML application

    Some options that I am aware of:

    • Using singletons with QML_SINGLETON or qmlRegisterSingletonInstance(...)
    • Setting context properties with QQmlContext::setContextProperty(...)
    • Setting initial/required properties with QQmlApplicationEngine::setInitialProperties(...)
    1 Reply Last reply
    0
    • P peter.thompson
      26 Mar 2025, 19:17

      @JoeCFD Thanks for linking that!

      From what I can tell, that chart is mostly about registering types with QML (although it does touch on the singleton option). I'm curious about moving the actual data from C++ to QML. To be more specific, if my data is loaded up using some C++ code in a model layer, how do I get that data (which exists in C++ objects) into the QML context? What is the actual method I should call at that point, assuming all the necessary types are already registered? Or do I just add it all to a singleton, which is available from both C++ and QML?

      I ask this in the context that the majority of my app is Qt widgets. It will only slowly be replaced with QML, piece by piece. So the driving code is going to remain C++ for a while. And I'll have a bunch of QML contexts floating around for the various QQuickWidgets.

      J Offline
      J Offline
      JoeCFD
      wrote on 27 Mar 2025, 11:26 last edited by JoeCFD
      #4

      @peter-thompson

      You can use
      Q_PROPERTY in C++ classes to define properties for direct access from QML
      or
      Model-View Approach(for example table data)

      class LargeDataModel : public QAbstractTableModel {
          Q_OBJECT
      public:
          explicit LargeDataModel(QObject *parent = nullptr);
          
          int rowCount(const QModelIndex &parent = QModelIndex()) const override;
          int columnCount(const QModelIndex &parent = QModelIndex()) const override;
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
          QHash<int, QByteArray> roleNames() const override;
          
          void loadData(const QVector<QVector<QVariant>> &newData);
      
      private:
          QVector<QVector<QVariant>> m_data;
      };
      

      Look for models in Qt QML to fit your needs.

      or use singleton QtObject qml files to define global geometry or stylesheet, etc. data for global access of all qml files.

      P 1 Reply Last reply 27 Mar 2025, 18:11
      0
      • J Offline
        J Offline
        JoeCFD
        wrote on 26 Mar 2025, 19:10 last edited by
        #2

        https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html
        The flowchart is a good reference.

        P 1 Reply Last reply 26 Mar 2025, 19:17
        0
        • J JoeCFD
          26 Mar 2025, 19:10

          https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html
          The flowchart is a good reference.

          P Offline
          P Offline
          peter.thompson
          wrote on 26 Mar 2025, 19:17 last edited by peter.thompson
          #3

          @JoeCFD Thanks for linking that!

          From what I can tell, that chart is mostly about registering types with QML (although it does touch on the singleton option). I'm curious about moving the actual data from C++ to QML. To be more specific, if my data is loaded up using some C++ code in a model layer, how do I get that data (which exists in C++ objects) into the QML context? What is the actual method I should call at that point, assuming all the necessary types are already registered? Or do I just add it all to a singleton, which is available from both C++ and QML?

          I ask this in the context that the majority of my app is Qt widgets. It will only slowly be replaced with QML, piece by piece. So the driving code is going to remain C++ for a while. And I'll have a bunch of QML contexts floating around for the various QQuickWidgets.

          J 1 Reply Last reply 27 Mar 2025, 11:26
          0
          • P peter.thompson
            26 Mar 2025, 19:17

            @JoeCFD Thanks for linking that!

            From what I can tell, that chart is mostly about registering types with QML (although it does touch on the singleton option). I'm curious about moving the actual data from C++ to QML. To be more specific, if my data is loaded up using some C++ code in a model layer, how do I get that data (which exists in C++ objects) into the QML context? What is the actual method I should call at that point, assuming all the necessary types are already registered? Or do I just add it all to a singleton, which is available from both C++ and QML?

            I ask this in the context that the majority of my app is Qt widgets. It will only slowly be replaced with QML, piece by piece. So the driving code is going to remain C++ for a while. And I'll have a bunch of QML contexts floating around for the various QQuickWidgets.

            J Offline
            J Offline
            JoeCFD
            wrote on 27 Mar 2025, 11:26 last edited by JoeCFD
            #4

            @peter-thompson

            You can use
            Q_PROPERTY in C++ classes to define properties for direct access from QML
            or
            Model-View Approach(for example table data)

            class LargeDataModel : public QAbstractTableModel {
                Q_OBJECT
            public:
                explicit LargeDataModel(QObject *parent = nullptr);
                
                int rowCount(const QModelIndex &parent = QModelIndex()) const override;
                int columnCount(const QModelIndex &parent = QModelIndex()) const override;
                QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
                QHash<int, QByteArray> roleNames() const override;
                
                void loadData(const QVector<QVector<QVariant>> &newData);
            
            private:
                QVector<QVector<QVariant>> m_data;
            };
            

            Look for models in Qt QML to fit your needs.

            or use singleton QtObject qml files to define global geometry or stylesheet, etc. data for global access of all qml files.

            P 1 Reply Last reply 27 Mar 2025, 18:11
            0
            • J JoeCFD
              27 Mar 2025, 11:26

              @peter-thompson

              You can use
              Q_PROPERTY in C++ classes to define properties for direct access from QML
              or
              Model-View Approach(for example table data)

              class LargeDataModel : public QAbstractTableModel {
                  Q_OBJECT
              public:
                  explicit LargeDataModel(QObject *parent = nullptr);
                  
                  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
                  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
                  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
                  QHash<int, QByteArray> roleNames() const override;
                  
                  void loadData(const QVector<QVector<QVariant>> &newData);
              
              private:
                  QVector<QVector<QVariant>> m_data;
              };
              

              Look for models in Qt QML to fit your needs.

              or use singleton QtObject qml files to define global geometry or stylesheet, etc. data for global access of all qml files.

              P Offline
              P Offline
              peter.thompson
              wrote on 27 Mar 2025, 18:11 last edited by peter.thompson
              #5

              @JoeCFD Thanks for your answer. From what I understand, that helps to explain what's required in a pure QML application.

              I also had a second question, regarding an application that is hybrid QML and Qt widgets (using QQuickWidget). But I think that maybe this got lost in my original question, so I will ask a new question specifically about the hybrid scenario.

              Link to the new question: https://forum.qt.io/topic/161561/what-is-the-best-practice-for-passing-c-data-to-qml-contained-in-a-qquickwidget

              J 1 Reply Last reply 27 Mar 2025, 18:54
              0
              • P peter.thompson has marked this topic as solved on 27 Mar 2025, 18:12
              • P peter.thompson
                27 Mar 2025, 18:11

                @JoeCFD Thanks for your answer. From what I understand, that helps to explain what's required in a pure QML application.

                I also had a second question, regarding an application that is hybrid QML and Qt widgets (using QQuickWidget). But I think that maybe this got lost in my original question, so I will ask a new question specifically about the hybrid scenario.

                Link to the new question: https://forum.qt.io/topic/161561/what-is-the-best-practice-for-passing-c-data-to-qml-contained-in-a-qquickwidget

                J Offline
                J Offline
                JoeCFD
                wrote on 27 Mar 2025, 18:54 last edited by JoeCFD
                #6

                @peter-thompson Better not to mix them. I mixed only one gstreamer qml sink in a widgets app. It is doable, but messy.

                1 Reply Last reply
                0

                5/6

                27 Mar 2025, 18:11

                • Login

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