Important: Please read the Qt Code of Conduct -

Is it possible to use QML ListView as "pagination"

  • Is there a way to handle large amount of data with a ListView to reduce server load? Generally speaking, something like a pagination, but not the standard type where user selects Page 1, 2 ,3... but rather request data while scrolling.

    I know that when using a ListView items not in screen are not rendered, so that part is fine, but the issue is when requesting data from api. If I have a large data set, it takes some time to load it all and create the delegate for the ListView. So the general idea would be to use limit and offset on the api call, to request data only for the first x items, and if user scrolls, fetch data for the next x items.

    Since I can't count on currentIndex (because the user can scroll the list without selecting/changing the current item) I can't use it to calculate limit/offset.

    I know the total count of items in the list, so I could try to limit the ListView model to e.g. 20 items, and when the user scrolls to the end, request next 20 items and extend the model (if that's possible).

    All in all, I was hoping for some suggestions and feedback on the idea, or if anyone had some similar case, how did they handle it?

  • Moderators

    hi @Wiru
    if you have an AbstractItemModel (c++) as a base for your model, you can/should implement the canFetchMoreand fetchMore methods for a dynamic loading of the data.

    The this example

    for a more detailed view

  • @J-Hilk I'm handling the api call in javascript and create an object that's then used as the model for the ListView. I was also considering in making a custom ListView component based on the ListView class. But if possible, I'd rather not, as it would increase the complexity.

    Edit: This fetchMore definitely seems like something that would be great to have. I'll check it out more.

  • @J-Hilk Quick question regarding beginInsertRows(), do you by chance know if it resets the ListViws current item/index? It's important for me to keep the selected item intact, e.g. if current index is 5, and it inserts 10 more items, it keeps item at index 5 selected.

    I did some research about dynamic ListView models, but in all examples, once the model is extended/updated, ListView resets to index 0, losing users selected item.

  • Moderators

    I don't know for sure, but I think it simply resets the currentItem/Index

    I'm not an expert in the model view field, so maybe someone else can give you a better/more detailed answer

  • This post is deleted!

Log in to reply