How to create custom icons for QFileSystemModel in a background thread
Asked Answered
K

2

7

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.

Katonah answered 25/8, 2016 at 11:55 Comment(1)
If you have preview generating code packaged up as a function then look into passing it to QtConcurrent::run for background execution and then using queued signals for notification.Wadesworth
G
11

Unfortunately, there's an impedance mismatch between the QFileIconProvider API and the model api: the QFileSystemModel 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.

// https://github.com/KubaO/stackoverflown/tree/master/questions/icon-proxy-39144638
#include <QtWidgets>
#include <QtConcurrent>

/// A thread-safe function that returns an icon for an item with a given path.
/// If the icon is not known, a null icon is returned.
QIcon getIcon(const QString & path);

class IconProxy : public QIdentityProxyModel {
    Q_OBJECT
    QMap<QString, QIcon> m_icons;
    Q_SIGNAL void hasIcon(const QString&, const QIcon&, const QPersistentModelIndex& index) const;
    void onIcon(const QString& path, const QIcon& icon, const QPersistentModelIndex& index) {
        m_icons.insert(path, icon);
        emit dataChanged(index, index, QVector<int>{QFileSystemModel::FileIconRole});
    }
public:
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override {
        if (role == QFileSystemModel::FileIconRole) {
            auto path = index.data(QFileSystemModel::FilePathRole).toString();
            auto it = m_icons.find(path);
            if (it != m_icons.end()) {
                if (! it->isNull()) return *it;
                return QIdentityProxyModel::data(index, role);
            }
            QPersistentModelIndex pIndex{index};
            QtConcurrent::run([this,path,pIndex]{
                emit hasIcon(path, getIcon(path), pIndex);
            });
            return QVariant{};
        }
        return QIdentityProxyModel::data(index, role);
    }
    IconProxy(QObject * parent = nullptr) : QIdentityProxyModel{parent} {
        connect(this, &IconProxy::hasIcon, this, &IconProxy::onIcon);
    }
};
Garnish answered 25/8, 2016 at 21:21 Comment(6)
Than you for your response. I tried your solution and it seems to work. But now I have problems on accessing my QFileSystemModel. 1. I am setting my QFileSytemModel to my QIdentityProxyModel by calling setSourceModel 2. Iam setting my QFileIdentityModel to my QListView by calling setModel and I have a crush on runtime while I am trying to access a filePath of a selected QModelIndex of my QFileSystemModel. Any ideas on what am I doing wrong?Katonah
I am answering my own question: It was my mistake, I was trying to access items of QFileSystemModel by using QModelIndexes from my QListView, which has been set to my QIdentityProxyModel. The correct way is to access items directly from QIdentityProxyModel by using the QModelIndex that your QListView returns you (since a QIdentityProxyModel is set to your QList View).Katonah
You're right. Indexes belong to a particular model. When you're using a proxy, you should forget that the source model exists, pretty much. The proxy is what you use - there being a source model is an implementation detail :)Whitaker
@KubaOrder: I there any way to return the QFileSystemModel's default icon (for example in case of a folder)? I am trying to make getIcon to return 'QIdentityProxyModel::data(index, QFileSystemModel::FileIconRole).value<QIcon>();' in case of folder, but it does not work.Katonah
I've updated the answer. getIcon doesn't know anything about any models. If it doesn't know how to obtain an icon, it can return a default-constructed one, and the proxy will forward the request to the source model for a default icon, then.Whitaker
@KubaOrder: Yes! that was the trick. The return QIdentityProxyModel::data(index, role); in case of it->isNull() . Than you very much.Katonah
H
6

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:

  • Limit threads: Pass a QThreadPool to QConcurrent::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.
  • Avoid re-entry: Need to handle the case where the icon for the same path is queried multiple times before icon generation is complete. Current code would spawn multiple threads generating the same icon. Simple solution is to add a placeholder entry to the m_icons map before the QConcurrent::run call. I just called to the default QIdentityProxyModel::data(index, QFileSystemModel::FileIconRole), so the icon gets a decent default before loading is complete
  • Task cancellation: If you destroy your model (or want to switch view folders, etc.), you'll want a way to cancel the active tasks. Unfortunately, there's no built-in way to cancel a pending QConcurrent::run task. I used a std::atomic_bool to signal cancellation, which the tasks check before executing. And a std::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 to setScaledSize. Note that if you have non-square images, you'll want to pass a size with the appropriate aspect ratio like this:

    const QSize originalSize = reader.size(); // Note: Doesn't load the file contents
    QSize scaledSize = originalSize;
    scaledSize.scale(MaximumIconSize, Qt::KeepAspectRatio);
    reader.setScaledSize(scaledSize);
Hf answered 30/3, 2017 at 16:20 Comment(1)
Do you have an idea how I can pass the options.rect from a ItemDelegate::paint() to the data model, so the model can handle the thumbnail generation? I need scaled+cropped thumbnails fitting the rect inside a ItemDelegate.Girder

© 2022 - 2024 — McMap. All rights reserved.