I am making a file browser in qt for some custom design-files. I want to load their preview as their thumbnail and for that reason I am using QIconProvider
to return the Icon to my QFileSystemModel
.
The problem is that the algorithm that creates the QIcon
needs some resources and as a result my application is not responsive until it finishes loading all the thumbnails.
I am wondering if there is any way to put my QIconProvider
in a background thread, so that I have my application responsive.
The accepted answer is fantastic - introduced me to some of the more advanced Qt concepts.
For anyone trying this in the future, here's some changes I had to make to get this working smoothly:
QThreadPool
toQConcurrent::run
, with max threads set to 1 or 2. Using the default killed the app, as all threads get burned building image previews. Bottleneck will be disk, so doesn't make sense to have more than 1 or 2 threads on this task.QIdentityProxyModel::data(index, QFileSystemModel::FileIconRole)
, so the icon gets a decent default before loading is completeQConcurrent::run
task. I used astd::atomic_bool
to signal cancellation, which the tasks check before executing. And astd::condition_variable
to wait on until all tasks are cancelled/complete.Tip: My use case for this was to load thumbnail previews from images on disk (likely the common use case). After some experimentation, I found that the fastest way to generate previews is to use
QImageReader
, passing your thumbnail size tosetScaledSize
. Note that if you have non-square images, you'll want to pass a size with the appropriate aspect ratio like this:Unfortunately, there's an impedance mismatch between the
QFileIconProvider
API and the model api: theQFileSystemModel
provides asynchronous notifications to the view when things change, but the icon provider can't asynchronously notify the model when icons change or become known.You can install an identity proxy between the file system model and the view(s). That proxy's
data
method would then query the icons asynchronously. The model's synchronous icon provider is then unused and unnecessary.