Kowalski Paweł Kowalski Paweł - 3 days ago 6
Python Question

How to remove an item from QFileSystemModel in QTreeView (PySide)

I cannot figure it out, how to remove an item or an row from QFileSystemModel?
After using removeRows nothing happens. I've also tried

self.model.beginRemoveRows(QtCore.QAbstractItemModel.index(), 0, 10)
self.model.endRemoveRows()


With no result. How can I do it?

Regards

Answer

QFileSystemModel doesnt follow the conventional functions for removing items, I guess because it is so potentially destructive.

you need to call self.model.remove(index) for each one. This will permanently delete the files. You also need to call self.model.rmdir(index) if it happens to be a folder.

see http://doc.qt.io/qt-4.8/qfilesystemmodel.html#remove

If you are just trying to hide the rows, then you can add a list of filters e.g. ["*.cpp", "*.h"] via QFileSystem.setNameFilters, which uses QDir::setNameFIlters.

If you are doing something more specialised, then you can use QSortFilterProxy. Either use it directly with a QRegExp, or subclass and implement filterAcceptsRow()

Comments