我已经为此苦苦挣扎了一段时间。
由于非常糟糕的图标获取算法, QtQFileSystemModel
在获取数百个文件时非常慢。
我想完全禁用图标。它们以QFileSystemModel::data
非虚拟的方法获取(QFileSystemModel 的源代码可在此处获得:http: //qt.gitorious.org/qt/qt/blobs/4.7/src/gui/dialogs/qfilesystemmodel.cpp)
这是我试图运行的代码:
class FileModel : public QFileSystemModel {
using QFileSystemModel::data;
public:
QVariant QFileSystemModel::data(const QModelIndex &index, int role) const
{
// my own implementation here
}
}
但它失败了
cannot define member function QFileSystemModel::data witin FileModel
更新
我已经设法覆盖了该功能,这就是我现在所拥有的:
class FileModel : public QFileSystemModel {
public:
QVariant data(const QModelIndex &index, int role) const
{
// Here goes Qt's implementation
Q_D(const QFileSystemModel);
if (!index.isValid() || index.model() != this)
return QVariant();
switch (role) {
case Qt::EditRole:
case Qt::DisplayRole:
switch (index.column()) {
case 0: return d->name(index);
case 1: return d->size(index);
case 2: return d->type(index);
case 3: return d->time(index);
default:
qWarning("data: invalid display value column %d", index.column());
break;
}
break;
case FilePathRole:
return filePath(index);
case FileNameRole:
return d->name(index);
case Qt::DecorationRole:
if (index.column() == 0) {
QIcon icon = d->icon(index); // This is the part I need to change
if (icon.isNull()) {
if (d->node(index)->isDir())
icon = d->fileInfoGatherer.iconProvider()->icon(QFileIconProvider::Folder);
else
icon = d->fileInfoGatherer.iconProvider()->icon(QFileIconProvider::File);
}
return icon;
}
break;
case Qt::TextAlignmentRole:
if (index.column() == 1)
return Qt::AlignRight;
break;
case FilePermissions:
int p = permissions(index);
return p;
}
return QVariant();
}
};
但是,此代码无法编译。这是我得到的错误:
'const QFileSystemModelPrivate* QFileSystemModel::d_func() const' is private