1

I cannot figure out how to hide an item or a 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?

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Kowalski Paweł
  • 594
  • 1
  • 6
  • 27

2 Answers2

4

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()

Joseph Ireland
  • 2,465
  • 13
  • 21
  • Thanks, I've got to read a little more on this subject. But cannot I just hide an item from the view? Because, in fact, that's what I'm trying to do. – Kowalski Paweł Nov 30 '16 at 16:12
  • 1
    QFileSystemModel supports simple name filters (added to answer), or use more complex ones with QSortFilterProxy. Don't think you can just remove row 1. – Joseph Ireland Nov 30 '16 at 16:26
3

As an aside: You're never supposed to call the beginXxx and removeXxx methods as the user of a model. The model itself is supposed to invoke those.

This is an error in the PySide wrapper's API design. In C++, those methods are protected. Python has no concept of a protected method per se, and the implementers of PySide evidently chose to expose protected methods using unadorned names in spite of Python conventions. In Python, it might be conventional to prefix protected methods with a single underscore _. The private methods use a double underscore __, but that wouldn't matter since they wouldn't propagate into they Pythonic API from C++.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • The [double-underscore](http://docs.python.org/3/reference/expressions.html#atom-identifiers) isn't merely a convention - it's part of the language specification. Double-underscored names are automatically mangled in order to avoid clashes between "private" attributes of base and derived classes. There is no real convention for single-underscored names - for example, the standard library [namedtuple](https://docs.python.org/3/library/collections.html#collections.somenamedtuple._make) class uses them as part of its public API. – ekhumoro Nov 30 '16 at 21:34