Cnewbiec
Cnewbiec

Reputation: 71

PyQt5 hide folders in QTreeView

I have something like this :

self.model = QFileSystemModel()
self.model.setRootPath(self.dirPath)

self.tree = QTreeView()
self.tree.setModel(self.model)
self.tree.setRootIndex(self.model.index(os.getcwd()))

and I wonder how to not display folders at all, only files. I did set NameFilters and that worked as a filter for files but it was still showing up a folders. I don't want to see any folders in selected directories.

Upvotes: 0

Views: 510

Answers (1)

musicamante
musicamante

Reputation: 48231

Setting the name filter won't suffice, as filtering by object type obviously has nothing to do with the object name.

If you just scroll through the documentation, you'll see that there's also a setFilter() function that uses the QDir.Filter flags; the Files filter is exactly what you need to only show files.

    self.model.setFilter(QtCore.QDir.Files)

Since hiding directories obviously makes impossible to show the tree hierarchy, using a QTreeView is pointless.
If you only want to show file names, then you can use a QListView, otherwise QTableView can be used in order to also display file details.

Upvotes: 1

Related Questions