How to populate a ListModel without for loop

  • 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:


    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]);


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


    QObject *obj = my_widget->rootObject();
                                  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?

  • 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 :-)

  • @Davide87 If you want better performance you need to use C++. QAbstractListModel could do the job I think. You can use this "smart models" if you don't want to implement your own model.

  • Thank you daljit. I'll try it.

Log in to reply