- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 qt 中为一些自定义设计文件制作一个文件浏览器。我想将它们的预览加载为缩略图,因此我使用 QIconProvider
将图标返回到我的 QFileSystemModel
。
问题是创建 QIcon
的算法需要一些资源,因此我的应用程序在完成加载所有缩略图之前没有响应。
我想知道是否有任何方法可以将我的 QIconProvider
放入后台线程中,以便我的应用程序能够响应。
最佳答案
不幸的是,QFileIconProvider
API 和模型 API 之间存在阻抗不匹配:QFileSystemModel
在情况发生变化时向 View 提供异步通知,但图标提供程序却不能当图标更改或变为已知时,异步通知模型。
您可以在文件系统模型和 View 之间安装身份代理。然后,该代理的 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);
}
};
关于qt - 如何在后台线程中为 QFileSystemModel 创建自定义图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39144638/
我正在寻找的几乎与 QFileSystemModel::setNameFilters() 相反。我想设置过滤器来捕获我不想显示的内容,但让其他所有内容通过。我看不出有什么方法可以遍历和隐藏某些文件,因
我可以扩展 QFileSystemModel 并添加带有文本/图标的新列吗? 问候 最佳答案 我将从子类化模型开始,提供额外的列并向其提供数据。 所以至少我会在这两种情况下重新实现 columnCou
我正在使用带有 QListview 的 QFileSystemModel 来显示目录中的所有文件。我想过滤该模型以显示某些类别的文件,例如: 文本文件:*.txt *.csv *.tab 音乐:*.m
我可以扩展 QFileSystemModel 并添加带有文本/图标的新列吗? 问候 最佳答案 我将从子类化模型开始,提供额外的列并向其提供数据。 所以至少我会在这两种情况下重新实现 columnCou
我正在使用以下过滤器设置我的 QFileSystemModel: QDir::Filters( Dirs|AllDirs|Files|Drives|NoDotAndDotDot|AllEntries
我正在尝试创建一个文件资源管理器,您可以在其中查找文件。找到后,用户应该能够选择他要上传的文件。因此我需要所选文件的路径。 这是我当前的代码: import sys from PyQt4.QtGui
在我的项目中,我有一个 QTreeView 显示我的驱动器上的一个位置。我需要将文件的所有图标更改为自定义图标,但不要管文件夹。 我重新实现了 QFileSystemModel,并且能够更改所有图标。
我正在尝试创建一个显示文件夹内容的 Qt 应用程序(在 Mac OS 中为“Users”文件夹)。这是代码: QFileSystemModel *dirModel = new QFileSystemM
如何像在 QDirModel 中那样使用 QDir::DirsFirst 对 QFileSystemModel 进行排序?QFileSystemModel 没有setSorting 方法。 最佳答案
所以我有一个 TreeView ,如下所示; #QTreeView widget #Shows files in set directory self.treeView = Q
我正在开始 Qt/pySide 编程,并试图实现一个简单的 QListView,以 QFileSystemModel 作为模型。我有这个工作,另外在模型上定义了一个名称过滤器。我想获得 QListVi
我正在尝试使用 QFileSystemModel 和 QTableView 制作一个小文件浏览器。 我想知道是否可以在“修改日期”列中突出显示具有相同值的行,例如,如果我今天修改了两个或多个文件,行以
是否可以使用 QFileSystemModel只列出一个目录内容而不是我计算机中的所有驱动器(在 Windows 中)? 最佳答案 这将是您的 View 的功能,而不是您的模型: http://doc
我正在 qt 中为一些自定义设计文件制作一个文件浏览器。我想将它们的预览加载为缩略图,因此我使用 QIconProvider 将图标返回到我的 QFileSystemModel。 问题是创建 QIco
您好,我已经编写了这段基本代码,试图填充 /Users/ 目录下的文件夹,但我不知道我错过了什么,它没有填充。 import sys from PyQt4 import QtGui from PyQt
首先,这个问题与另一个问题类似QFileSystemModel not updating when files change与本例的主要区别是,我不想每次更新我的文件之一时都替换整个模型。 在现实世界
通过环顾四周,我发现 QFileSysetmModel 中的 QFileIconProvider 会大大减慢速度。在我的特殊情况下,我根本不需要它,但我不知道如何轻松地禁用/删除它而不导致崩溃。这是我
我使用 QFileSystemModel 通过 QTreView 表示文件结构。一切正常,但我需要在树的某个级别添加额外的行。例如现在是: -根 --row1 --row2 --row3 所有这些行从
我遇到 QFileSystemModel 无法显示文件更改的问题。当文件第一次创建时,它会立即显示出来。但是当文件本身发生变化时,大小和时间戳不会更新。我已经多次尝试强制模型更新,但没有真正成功。我所
我有一个 QFileSystemModel 用作 QTreeView 中的模型。这个工作正常,现在我有一些目录的名称。我想将其隐藏在 QTreeview 中。 QString strCompleteD
我是一名优秀的程序员,十分优秀!