如何在后台线程中为QFileSystemModel创建自定义图标

mic*_*lis 6 qt thumbnails qfilesystemmodel background-thread qicon

我在qt中为一些自定义设计文件制作文件浏览器.我想加载他们的预览作为他们的缩略图,因此我QIconProvider用来将Icon返回给我QFileSystemModel.

问题是创建的算法QIcon需要一些资源,因此我的应用程序在完成加载所有缩略图之后才会响应.

我想知道是否有任何方法可以将我QIconProvider放在后台线程中,以便我的应用程序响应.

Rei*_*ica 8

不幸的是,QFileIconProviderAPI与模型api 之间存在阻抗不匹配:QFileSystemModel当事情发生变化时,会向视图提供异步通知,但图标提供程序在图标更改或已知时不能异步通知模型.

您可以在文件系统模型和视图之间安装标识代理.data然后,该代理的方法将异步查询图标.然后,模型的同步图标提供程序未使用且不必要.

// 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);
    }
};
Run Code Online (Sandbox Code Playgroud)


Ale*_*erg 5

可接受的答案是极好的-向我介绍了一些更高级的Qt概念。

对于将来尝试此操作的任何人,为了使此操作顺利进行,我必须进行一些更改:

  • 限制线程数:将传递QThreadPoolQConcurrent::run,最大线程数设置为1或2。使用默认值终止该应用程序,因为所有线程都被烧毁,生成建筑物图像预览。瓶颈将是磁盘,因此在此任务上拥有1个或2个以上的线程没有任何意义。
  • 避免重新输入:需要处理在图标生成完成之前多次查询同一路径的图标的情况。当前代码将产生多个线程以生成相同的图标。一个简单的解决方案是在QConcurrent :: run调用之前在m_icons映射中添加一个占位符条目。我只是调用了default QIdentityProxyModel::data(index, QFileSystemModel::FileIconRole),所以图标在加载完成之前得到了不错的默认值
  • 任务取消:如果销毁模型(或要切换视图文件夹等),则需要一种取消活动任务的方法。不幸的是,没有内置的方法可以取消待处理的QConcurrent::run任务。我使用std::atomic_bool来表示取消,这是任务在执行之前检查的。和std::condition_variable等待,直到所有的任务被取消/完成。

提示:我的用例是从磁盘上的图像加载缩略图预览(可能是常见用例)。经过一些试验,我发现生成预览的最快方法是使用QImageReader,将缩略图大小传递给setScaledSize。请注意,如果您有非正方形图像,则需要使用适当的宽高比传递尺寸,如下所示:

    const QSize originalSize = reader.size(); // Note: Doesn't load the file contents
    QSize scaledSize = originalSize;
    scaledSize.scale(MaximumIconSize, Qt::KeepAspectRatio);
    reader.setScaledSize(scaledSize);
Run Code Online (Sandbox Code Playgroud)