Search code examples
pythonpyqtpyqt5qtreeviewqfilesystemmodel

Hide Size, Type and Date modified columns in QFileSystemModel


I want to remove the following:

enter image description here

I just want to show 'only' the file names.

This is my QTreeView Code:

self.model = QFileSystemModel()
self.model.setRootPath(QDir.rootPath())
self.model.setFilter(QDir.NoDotAndDotDot | QDir.AllEntries | QDir.Dirs | QDir.Files)
self.proxy_model = QSortFilterProxyModel(recursiveFilteringEnabled = True, filterRole = QFileSystemModel.FileNameRole)
self.proxy_model.setSourceModel(self.model)
self.model.setReadOnly(False)
self.model.setNameFilterDisables(False)

self.indexRoot = self.model.index(self.model.rootPath())

self.treeView = QTreeView(self)
self.treeView.setModel(self.proxy_model)
self.adjust_root_index()

self.treeView.setRootIndex(self.indexRoot)
self.treeView.clicked.connect(self.on_treeView_clicked)
self.treeView.doubleClicked.connect(self.treeMedia_doubleClicked)
self.treeView.setDragDropMode(QAbstractItemView.InternalMove)
self.treeView.setAnimated(True)
self.treeView.setIndentation(20)
self.treeView.setSortingEnabled(True)
self.treeView.setDragEnabled(True)
self.treeView.setAcceptDrops(True)
self.treeView.setDropIndicatorShown(True)
self.treeView.setEditTriggers(QTreeView.NoEditTriggers)
self.treeView.setContextMenuPolicy(Qt.CustomContextMenu)
self.treeView.customContextMenuRequested.connect(self.showContextMenu)

I've tried some options from the PyQt5 API, like setHeader() But I couldn't figure out how it quite works. I'm not sure if setHeader() is even what I'm looking for.


Solution

  • You can hide the columns using the hideSection() method of the QHeaderView:

    for i in range(1, self.treeView.model().columnCount()):
        self.treeView.header().hideSection(i)