本文介绍了如何覆盖(重新实现)QFileSystemModel中的成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力这一段时间。



Qt的 QFileSystemModel 因为一个非常糟糕的图标提取算法。



我想完全禁用图标。它们在 QFileSystemModel :: data 方法中获取,它不是虚拟的(QFileSystemModel的源在这里可用:)



这是我试图运行的代码:

  class FileModel:public QFileSystemModel {
using QFileSystemModel :: data;
public:
QVariant QFileSystemModel :: data(const QModelIndex& index,int role)const
{
//我自己实现这里
}
}

但是失败



code>无法定义成员函数QFileSystemModel :: data FileModel



UPDATE b
$ b

我已经设法覆盖该函数,这是我现在的:

  class FileModel:public QFileSystemModel {
public:
QVariant数据(const QModelIndex& index,int role)const
{
//这里Qt的实现
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);
默认值:
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); //这是我需要改变的部分
if(icon.isNull()){
if(d-> node(index) - > isDir())
icon = d-> fileInfoGatherer.iconProvider() - >图标(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'是私人的

解决方案

看起来是这样的,因为有一个方法来设置图标提供者:





参数, QFileIconProvider 看起来是一个相当简单的类,一个自己的实例,使用从 QFileInfo (基本上,文件名)获取图标的方法:





您可以实现其中之一,只为所有内容返回相同的图标。如果你发现没有解决你的问题,以下编译的罚款为我... FWIW:

  class FileModel:public QFileSystemModel {
public:
QVariant data(const QModelIndex& index,int role)const
{
if(role == Qt :: DecorationRole){
return QVariant(QIcon());
}

return QFileSystemModel :: data(index,role);
}
};


I've been struggling with this for a while.

Qt's QFileSystemModel is really slow when fetching several hundred files because of a really bad icon fetching algorithm.

I want to completely disable icons. They are fetched in QFileSystemModel::data method which is not virtual (the source of QFileSystemModel is available here: http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/dialogs/qfilesystemmodel.cpp)

This is the code I'm trying to run:

class FileModel : public QFileSystemModel {
    using QFileSystemModel::data;
public:
    QVariant QFileSystemModel::data(const QModelIndex &index, int role) const
    {
     // my own implementation here
    }
}

but it fails with

cannot define member function QFileSystemModel::data witin FileModel

UPDATE

I've managed to override the function and this is what I have now:

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();
}
};

However this code doesn't compile. This is the error I get:'const QFileSystemModelPrivate* QFileSystemModel::d_func() const' is private

解决方案

Looks like this was anticipated, as there is a method for setting the "icon provider":

http://doc.qt.io/archives/qt-4.7/qfilesystemmodel.html#setIconProvider

The parameter, a QFileIconProvider, looks to be a fairly simple class that you can implement an instance of yourself, with a method that fetches an icon from a QFileInfo (basically, a file name):

http://doc.qt.io/archives/qt-4.7/qfileinfo.html

You could implement one of these that just returns the same icon for everything. If you find that doesn't address your problem, the following compiled fine for me...FWIW:

class FileModel : public QFileSystemModel {
public:
    QVariant data(const QModelIndex &index, int role) const
    {
        if (role == Qt::DecorationRole) {
            return QVariant (QIcon ());
        }    

        return QFileSystemModel::data(index, role);
    }
};

这篇关于如何覆盖(重新实现)QFileSystemModel中的成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:49