불행하게도, 거기 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);
}
};
미리보기 생성 코드를 함수로 패키지화 한 경우 [ 'QtConcurrent :: run'] (http://doc.qt.io/qt-5/qtconcurrentrun.html)를 실행 한 다음 알림을 위해 대기중인 신호를 사용합니다. 대답을 위해 –