【问题标题】:How to add custom row in QFileSystemModel?如何在 QFileSystemModel 中添加自定义行?
【发布时间】:2012-02-11 17:41:15
【问题描述】:

我正在使用 QFileSystemModel 通过 QTreView 来表示文件结构。一切正常,但我需要在树的某个级别添加一个额外的行。例如现在是:

-根

--row1

--row2

--row3

所有这些行都从文件系统映射文件夹/文件。 我需要:

-根

--row1

--row2

--row3

--自定义行

所以自定义行不代表来自文件系统的任何数据。我只需要在这里添加我自己的数据。 我从互联网上阅读了很多东西,人们建议使用代理模型并重新实现 rowCount()、data() 和 flags() 函数。我试图这样做(使用从 QSortFilterProxyModel 派生的类),但我从来没有在 data() 和 flags() 函数中得到我的行。似乎它从源模型中计算出来。

QVariant AddonFilterModel::data (const QModelIndex & index, int role) const
{
    if(role == Qt::DisplayRole && index.row() == FilterModel::rowCount(index))
    {
        return QString("Add-Ons");
    }

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

Qt::ItemFlags AddonFilterModel::flags(const QModelIndex & index) const
{
    if (!index.isValid())
        return 0;

    if (index.row() == FilterModel::rowCount(index))
    {
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    }

    return FilterModel::flags(index);
}

int AddonFilterModel::rowCount(const QModelIndex &parent) const
{
    int count = FilterModel::rowCount(parent);

    if(parent == this->getRootIndex())
    {
        return count+1;
    }
    return count;
}

使用从 QAbstractProxyModel 派生的类是不可接受的,因为我需要 QSortFilterProxyModel() 的过滤功能。

我还尝试重新实现 QFileSystemModel 的 rowCount() 以直接在模型中进行更改,但我从 QT 代码中收到“数组超出范围”错误。

我尝试过 insertRow() 方法,但它不起作用。我认为是因为 QFileSystemModel 是只读的。

有人遇到过这个问题吗?有什么想法吗?

【问题讨论】:

    标签: qtreeview qfilesystemmodel


    【解决方案1】:

    迟到的答案。你必须继承 Qabstractitemmodel。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      相关资源
      最近更新 更多