Important: Please read the Qt Code of Conduct -

QListView only load visible items?

  • Hello everyone,

    I have a questions regarding listviews in QT 5 without QML.

    I have a iterator based C API that contains about 5000 items. Now I want to display them inside a QListView.
    I introduced a class inheriting the QAbstractListModel as the delegate and added the model to the view.

    The first thing I do not really understand is, why all items are loaded after I add the model to the listview. In terms of performance it would preferable to only load the visible items. After initially the data function is called ~5000 times further scrolling through the list only updates the surrounding rows (as expected)

    Are all rows in memory after adding the model to the view?

    I also tried the fetchMore and canFetchMore approach, but then I do not know the total count of rows at the beginning and the scrollbar is not correct.

    Greetings tore

  • How did you determine that all items are loaded when you add the model?

  • I added debug messages to the data function.

  • I'm not seeing this behaviour on my implementation. Are you sure the data function calls are the result of the ListView?

  • What does your rowCount function return?
    If it returns 5000, the data function gets called 5000 times for me.

    I actually don't do anything more with the model than adding it to the view (view->setModel( model ).

    Am I missing anything else?

  • Hi,
    What is the problem? Maybe the View just checks if all data elements are there and available so it doesn't crash when it doesn't get proper data before it starts showing information. Maybe you call the data function yourself?
    When the view only calls data when scrolling it is working great and gives great performance by only updating the data in view.
    Do you call the reset signal? How do you fill the model with data?

  • Hello again,

    aehm the problem is, that I would like to know how the view actually retrieves the data from the model.

    On a development PC it is no problem to have a list with about 5k items loaded at the beginning, but if the list gets bigger and the applications runs on an embedded device I want to save as much resources as possible.

    What I want the view to do is simply:

    1. Call rowCount -> return 5k entries.
    2. Adapt the scrollbar to fit 5k entries
    3. Only load entries 1-20, because those are actually visible inside the view.


  • In that case you can either use the debugger to follow function calls or look at QListView code to see what is happening.

  • sorry for my ignorance but i did not get few things... how do you populate the model? what model it is? QTableModel, qSqlQueryModel... and so on.. what database are you using ?from where are you taking the data 5000 rows MySql, SQLite, ...All this info are useful in order to give a proper answer to your problem

  • I simply use the setModel method

    @AppQtServiceListModel model = new AppQtServiceListModel();
    ui->list_view->setModel( model );@

    The AppQtServiceListModel is based on a QAbstractListModel.

    After creating the model instance I call an external API function to set the totalCount. -> rowCount now returns the already mentioned ~5k entries.

    Now the data function gets called:

    @QVariant AppQtServiceListModel::data( const QModelIndex &index, int role ) const
    if ( ! index.isValid() )
    return QVariant();

    if ( index.row() >= servicesTotal || index.row() < 0 )
    return QVariant();

    if ( role == Qt::DisplayRole )
    qDebug() << "index row " << index.row();

    /* Set the cursor of the external API to match the row index */
    ListSetCursorIndex( list, index.row() );
    return a QVariant with the desired value...

    return QVariant();

    Thats basically all.

Log in to reply