如何在QFileSystemModel中添加自定义行? [英] How to add custom row in QFileSystemModel?
问题描述
我正在使用QFileSystemModel通过QTreView表示文件结构.一切工作正常,但我需要在树的某个级别添加额外的一行.例如,现在是:
I am using QFileSystemModel to represent file structure through the QTreView. Everything works fine, but I need to add an additional row at some level of the tree. For example for now is:
-root
-第1行
-row2
-row3
所有这些行都映射文件系统中的文件夹/文件. 我需要:
All these rows mapping folders/files from file system. I need:
-root
-第1行
-row2
-row3
-自定义行
因此,自定义行不代表文件系统中的任何数据.我只需要在这里添加我自己的数据. 我已经从互联网上读到很多东西,人们建议使用代理模型并重新实现rowCount(),data()和flags()函数.我试图做到这一点(使用了从QSortFilterProxyModel派生的类),但是我从来没有在data()和flags()函数中得到我的行.似乎需要从源模型中计算出来.
So custom row is not representing any data from file system. I just need to add here my own data. I have read a lot of stuff from the internet and people advice to use proxy model and reimplement rowCount(), data() and flags() functions. I tried to do that(used class derived from QSortFilterProxyModel), but I never get my row in data() and flags() functions. Seems like it takes count from source model.
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()的过滤功能.
Using class derived from QAbstractProxyModel is not acceptable because I need filtering functions of QSortFilterProxyModel().
我还尝试重新实现QFileSystemModel的rowCount()以直接在模型中进行更改,但是我从QT代码中收到数组超出范围"错误.
Also I have tried to reimplement rowCount() of QFileSystemModel to make changes directly in model but I am getting "array out of range" error from QT code.
我尝试了insertRow()方法,但是它不起作用.我认为是因为QFileSystemModel是只读的.
I have tried insertRow() method but it is not working. I think because QFileSystemModel is read only.
有人遇到这个问题吗?有什么想法吗?
Did anyone face this problem? Any ideas?
推荐答案
最新答案.您必须继承Qabstractitemmodel.
Late answer. You have to subclass Qabstractitemmodel.
这篇关于如何在QFileSystemModel中添加自定义行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!