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 populate a ListModel without for loop
Forum Updated to NodeBB v4.3 + New Features

How to populate a ListModel without for loop

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 1.6k 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.
  • D Offline
    D Offline
    Davide87
    wrote on last edited by
    #1

    Hello everyone.
    I'm working on a Qt/QML software for data visualization that is meant to be quasi-real time.
    In the software I'm working on, I populate a ListModel object as follows:

    main.qml

    Test_Data {
        id: surfaceData
    }
    
    // some code
    
    function populate_model(x,y,my_data)
    {
        for(var i=0; i<array_data.length; i++)
            surfaceData.model.append({"row": y[i], "col": x[i], "value": my_data[i]);
    }
    

    Test_Data.qml

    import QtQuick 2.5
    
    Item {
        property bool isempty: true
        property alias model: dataModel
    
        ListModel {
            id: dataModel
        }
    }
    

    mainwindow.cpp

    QObject *obj = my_widget->rootObject();
    QMetaObject::invokeMethod(obj,"populate_model",
                                  Q_ARG(QVariant, QVariant::fromValue(array_x)),
                                  Q_ARG(QVariant, QVariant::fromValue(array_y)),
                                  Q_ARG(QVariant, QVariant::fromValue(array_data)));
    

    where my_widget is a QQuickWidget and array_x, array_y and array_data are std vectors.
    In short, is pass three arrays to the QML function and populate the ListModel with that for loop.
    The problem is that the arrays are generally very big (hundreds of thousands of elements) and populating such model list takes about one one second.

    Is it possible to avoid the for loop with the appends to make the populating process faster?

    B 1 Reply Last reply
    0
    • D Davide87

      Hello everyone.
      I'm working on a Qt/QML software for data visualization that is meant to be quasi-real time.
      In the software I'm working on, I populate a ListModel object as follows:

      main.qml

      Test_Data {
          id: surfaceData
      }
      
      // some code
      
      function populate_model(x,y,my_data)
      {
          for(var i=0; i<array_data.length; i++)
              surfaceData.model.append({"row": y[i], "col": x[i], "value": my_data[i]);
      }
      

      Test_Data.qml

      import QtQuick 2.5
      
      Item {
          property bool isempty: true
          property alias model: dataModel
      
          ListModel {
              id: dataModel
          }
      }
      

      mainwindow.cpp

      QObject *obj = my_widget->rootObject();
      QMetaObject::invokeMethod(obj,"populate_model",
                                    Q_ARG(QVariant, QVariant::fromValue(array_x)),
                                    Q_ARG(QVariant, QVariant::fromValue(array_y)),
                                    Q_ARG(QVariant, QVariant::fromValue(array_data)));
      

      where my_widget is a QQuickWidget and array_x, array_y and array_data are std vectors.
      In short, is pass three arrays to the QML function and populate the ListModel with that for loop.
      The problem is that the arrays are generally very big (hundreds of thousands of elements) and populating such model list takes about one one second.

      Is it possible to avoid the for loop with the appends to make the populating process faster?

      B Offline
      B Offline
      Bernd B
      wrote on last edited by
      #2

      @Davide87 Perhaps,
      you get the solution here: https://forum.qt.io/topic/91426/model-for-a-list-of-large-data-in-qml/8

      1 Reply Last reply
      2
      • D Offline
        D Offline
        Davide87
        wrote on last edited by
        #3

        Thank you for your answer Bernd. However that topic is not actually much of help for me :-(
        However, I'm thinking about leaving the QML idea and try another way to solve my problem.
        Thank you again :-)

        D 1 Reply Last reply
        0
        • D Davide87

          Thank you for your answer Bernd. However that topic is not actually much of help for me :-(
          However, I'm thinking about leaving the QML idea and try another way to solve my problem.
          Thank you again :-)

          D Offline
          D Offline
          daljit97
          wrote on last edited by
          #4

          @Davide87 If you want better performance you need to use C++. QAbstractListModel could do the job I think. You can use this "smart models" http://gitlab.unique-conception.org/qt-qml-tricks/qt-qml-models if you don't want to implement your own model.

          1 Reply Last reply
          2
          • D Offline
            D Offline
            Davide87
            wrote on last edited by
            #5

            Thank you daljit. I'll try it.

            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