QTreeView doesn't expand
Solved
Qt for Python
-
I'd like to display the tree expanded in QTreeView, and I'm using
expandAll()
. For some reason it doesn't work.
Here's the full working example (PySide2 can be replaced with PySide6):import PySide2 from PySide2 import QtWidgets from PySide2.QtWidgets import QFileSystemModel, QMainWindow, QWidget, QTreeView, QVBoxLayout from PySide2.QtCore import QDir, QSortFilterProxyModel, Qt import sys class View(QMainWindow): def __init__(self): super().__init__() self._w_main = QWidget() self.setCentralWidget(self._w_main) self.tree_view = QTreeView(self._w_main) self._layout = QVBoxLayout() self._layout.addWidget(self.tree_view) self._w_main.setLayout(self._layout) app = QtWidgets.QApplication([]) view = View() model = QFileSystemModel() model.setRootPath('.') model.setFilter(QDir.NoDot | QDir.AllEntries) model.sort(0, Qt.SortOrder.AscendingOrder) view.tree_view.setModel(model) view.tree_view.setRootIndex(model.index('.')) view.tree_view.header().setSortIndicator(0, Qt.AscendingOrder) view.tree_view.setSortingEnabled(True) view.showMaximized() view.tree_view.expandAll() sys.exit(app.exec_())
Am I missing something trivial? I also tried to
expand()
at some index, didn't work either. -
Hi
QFileSystemModel uses a thread to populate the model over time.
try to hook up to
https://doc.qt.io/qt-5/qfilesystemmodel.html#directoryLoaded
and expand there.