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 use QML ListModel in C++?
Forum Updated to NodeBB v4.3 + New Features

how to use QML ListModel in C++?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 4 Posters 878 Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I have a QML ListModel:

    ListModel {
        id: sceneFeatureListModel
    }
    
    Component.onCompleted: {
        for (var i = 0; i < sceneFeatureList.length; i++) {
            sceneFeatureListModel.append({ "uuid": sceneFeatureList[i] }) // this works.
        }
    }
    ...
    sceneModel.sendPatch(sceneCopy, sceneFeatureListModel) // this doesn't.
    

    My sendPatch signature is:

    void SceneModel::sendPatch(Scene *scene, QVariant qv) { 
    

    I can't figure out how what I should convert the QVariant to. I've tried lists and QObject, but neither work. Can I do this from this routine, or do I need to convert the ListModel contents before passing to C++?

    Thanks...

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by GrecKo
      #2

      I would recommend generally not using ListModel. The legit use cases are prototyping and a static model with actual roles. Populating a view only model from events is also fine.

      For other use cases, the better solution is a normal QAbstractItemModel, a QList or a JS array depending on the situation.

      To answer the question: you can pass it as a QAbstractListModel* .

      mzimmersM jeremy_kJ 2 Replies Last reply
      2
      • GrecKoG GrecKo

        I would recommend generally not using ListModel. The legit use cases are prototyping and a static model with actual roles. Populating a view only model from events is also fine.

        For other use cases, the better solution is a normal QAbstractItemModel, a QList or a JS array depending on the situation.

        To answer the question: you can pass it as a QAbstractListModel* .

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        @GrecKo said in how to use QML ListModel in C++?:

        the better solution is a normal QAbstractItemModel, a QList or a JS array

        OK, I used the JS array from which I created the ListModel - just updated it from the ListModel before sending it sendPatch().

        I would use a QAIM, but I can't figure out how to write the logic. The routine that initializes my JS array (and would be used to initialize a model) looks like this:

        QList<QString> SceneModel::featureUuidList(bool runningOnly, QUuid spaceUuid)
        {
            QList<QString> sceneFeatureList;
            QList<QString> featureUuids = m_activityModel->featureUuidList(runningOnly, spaceUuid);
            for (const auto &uuidString : featureUuids) {
                for (const auto &scene : m_list) {
                    for (const auto &feature : scene->featureList()) {
                        auto featureUuid = feature->featureId();
                        if (featureUuid.toString() == uuidString) {
                            auto featureIdString = featureUuid.toString();
                            if (!sceneFeatureList.contains(featureIdString))
                                sceneFeatureList.append(featureIdString);
                        }
                    }
                }
            }
            return sceneFeatureList;
        }
        

        I couldn't find a way to incorporate the spaceUuid into the selection logic.

        1 Reply Last reply
        0
        • GrecKoG GrecKo

          I would recommend generally not using ListModel. The legit use cases are prototyping and a static model with actual roles. Populating a view only model from events is also fine.

          For other use cases, the better solution is a normal QAbstractItemModel, a QList or a JS array depending on the situation.

          To answer the question: you can pass it as a QAbstractListModel* .

          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          @GrecKo said in how to use QML ListModel in C++?:

          I would recommend generally not using ListModel. The legit use cases are prototyping and a static model with actual roles.

          For people who mostly work with QML and javascript, I've taken to emphatically recommending ListModel. It works. Use it until testing determines that it does not work. It eliminates all of the questions about why a view fails to update when the incorrectly implemented or not at all valid model is updated.

          For C++ users, with an adequate understanding of the language, start with QStandardItemModel. It works. Use it until testing determines that it does not work. The same goes for Python, with the added performance benefit of spending less time in Python code.

          a QList or a JS array depending on the situation.

          These are better suited to testing and static data use cases. Over and over again, users are confused by the static model generated via ListView, TableView, etc failing to behave like a dynamic model.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          mzimmersM 1 Reply Last reply
          0
          • jeremy_kJ jeremy_k

            @GrecKo said in how to use QML ListModel in C++?:

            I would recommend generally not using ListModel. The legit use cases are prototyping and a static model with actual roles.

            For people who mostly work with QML and javascript, I've taken to emphatically recommending ListModel. It works. Use it until testing determines that it does not work. It eliminates all of the questions about why a view fails to update when the incorrectly implemented or not at all valid model is updated.

            For C++ users, with an adequate understanding of the language, start with QStandardItemModel. It works. Use it until testing determines that it does not work. The same goes for Python, with the added performance benefit of spending less time in Python code.

            a QList or a JS array depending on the situation.

            These are better suited to testing and static data use cases. Over and over again, users are confused by the static model generated via ListView, TableView, etc failing to behave like a dynamic model.

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

            @jeremy_k so, in regards to my original question, is there a way to pass a ListModel directly to C++ so it can be picked up as a QVariant, or am I correct to convert it to a JS array and pass that?

            Thanks...

            1 Reply Last reply
            0
            • F Offline
              F Offline
              flaudio
              wrote on last edited by
              #6

              Hi @mzimmers,
              try to pass the ListModel as QAbstractListModel *

              Q_INVOKABLE void yourMethod(QAbstractListModel *model) {
                      if (model) {
                          int rowCount = model->rowCount();
                        ...
              
              '''
              mzimmersM 1 Reply Last reply
              0
              • F flaudio

                Hi @mzimmers,
                try to pass the ListModel as QAbstractListModel *

                Q_INVOKABLE void yourMethod(QAbstractListModel *model) {
                        if (model) {
                            int rowCount = model->rowCount();
                          ...
                
                '''
                mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #7

                @flaudio I could do that, but I greatly want to keep that second parameter a QVariant. This function is eventually going to be used for a variety of purposes, and I'll need to convert that argument to several different things.

                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