简体   繁体   中英

How to hide an item from QFileSystemModel in QTreeView in PySide?

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?

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 eg ["*.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()

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++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM