Models and Views: Fetch More functionality using a worker thread

Demonstrates how to implement fetchMore() in a worker thread while maintaining a responsive UI.

This example shows how to utilize QAbstractItemModel::fetchMore() with an object moved to a QThread so that the data fetching does not block the UI. On each call, the FetchWorker sleeps for 2 seconds, to simulate a slow backend service, before sending more data to the UI thread.

Basic functionality

While data is being fetched in the worker thread, the model adds a BusyIndicator to the end of list. Once data is successfully fetched, the BusyIndicator is removed, and new items are appended to the list. The ListView is used in the typical way, and does not need adjustment to deal with the slow model.

Responsibilities

The item model changes (in this case inserting and removing rows) must happen in the UI thread. The worker thread object slowly constructs DataBlock structs, and emits the dataFetched signal with a QList of data blocks as the payload; the signal is sent via a Qt::QueuedConnection to the ThreadedFetchMoreModel::dataReceived() slot, which appends them to the data list in the UI thread. The UI thread adds a placeholder item to the end of the list before sending the fetchDataBlock() signal to the worker object to kick off the fetching process, and removes the placeholder before appending new items to the list.

After all available data is fetched, the worker thread object sends the noMoreToFetch signal to the model; from then on, the canFetchMore() method always returns false.

Example project @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.