English 中文(简体)
如何推翻(执行)在QFileSystemModel任职的成员
原标题:How to override (re-implement) a member function in QFileSystemModel

当时我一直在为此而努力。

Qt sQFileSystemModel, 因为在计算几百份档案时确实很慢,因为icon fetching算法确实坏。

我想说的是完全可以置信的讽刺。 页: 1 QFileSystemModel:datameth which is not actual (the source of QFileSystemModel is available here:

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
    }
}

但它失败了。

<条码> 成员职能的定义 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();
}
};

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

最佳回答

预计会出现这样的情况,因为有办法确定“提供方”:

参数,即<条码>QFileIconProvider,被视为一种非常简单的类别,可采用一种方法,从<条码>QFileInfo(主要为档案名称):

rel=“nofollow”http://doc.qt.io/archives/qt.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);
    }
};
问题回答

如果一个基类的功能是虚拟的,那么该功能在衍生类别中也是虚拟的。 下面将印刷“C”:

#include <iostream>

class A {
public:
  virtual void data() = 0;
};

class B: public A {
public:
  void data() { std::cout << "B
"; }
};

class C: public B {
public:
  void data() { std::cout << "C
"; }
};

int
main() {
  C c;
  A *a = &c;
  a->data();

  return 0;
}

<代码>QFileSystemDialog源自QAbstractItemModel,其中data(<>纯属虚拟。 即便是,如果它本身执行,它就没有超过<条码>数据()。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签