How to add custom string in QFileSystemModel?

I am using QFileSystemModel to represent the file structure through QTreView. Everything works fine, but I need to add an extra line at some level of the tree. For example, at the moment:

-root

- row1

- row2

- row3

All these lines display folders / files from the file system. I need:

-root

- row1

- row2

- row3

- custom string

Thus, the user string does not represent any data from the file system. I just need to add my data here. I read a lot of material from the Internet and people’s advice on using a proxy model and overriding the functions rowCount (), data () and flags (). I tried to do this (using a class derived from QSortFilterProxyModel), but I never get my string in the data () and flags () functions. It looks like he is taking the score from the original 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; } 

Using a class derived from QAbstractProxyModel is not acceptable since I need the filtering functions of QSortFilterProxyModel ().

I also tried to override rowCount () QFileSystemModel to make changes directly to the model, but I get an "array out of range" error from QT code.

I tried the insertRow () method, but it does not work. I think because QFileSystemModel is read-only.

Has anyone encountered this problem? Any ideas?

+4
source share
1 answer

Late answer. You must subclass Qabstractitemmodel.

0
source

Source: https://habr.com/ru/post/1391324/


All Articles