7

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

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

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

4

2 回答 2

11

QFileIconProvider不幸的是, API 和模型 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);
    }
};
于 2016-08-25T21:21:39.147 回答
6

公认的答案太棒了——向我介绍了一些更高级的 Qt 概念。

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

  • 限制线程:将 a 传递QThreadPoolQConcurrent::run,将最大线程数设置为 1 或 2。使用默认值会终止应用程序,因为所有线程都会烧毁构建图像预览。瓶颈将是磁盘,因此在此任务上拥有超过 1 或 2 个线程是没有意义的。
  • 避免重入:需要处理在图标生成完成之前多次查询同一路径的图标的情况。当前代码将产生多个线程,生成相同的图标。简单的解决方案是在 QConcurrent::run 调用之前向 m_icons 映射添加一个占位符条目。我刚刚调用了默认值QIdentityProxyModel::data(index, QFileSystemModel::FileIconRole),所以图标在加载完成之前得到了一个不错的默认值
  • 任务取消:如果您销毁模型(或想要切换视图文件夹等),您将需要一种取消活动任务的方法。不幸的是,没有内置的方法可以取消挂起的QConcurrent::run任务。我用 astd::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);
于 2017-03-30T16:20:58.697 回答