Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QTreeView doesn't expand
Qt 6.12 LTS is out! See what's new in the release blog

QTreeView doesn't expand

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    midnightdim
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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.

      M 1 Reply Last reply
      2
      • mrjjM mrjj

        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.

        M Offline
        M Offline
        midnightdim
        wrote on last edited by
        #3

        @mrjj Thanks a lot! This seems to work.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt
        • Unsolved