[solved] programatically expand a particular path in QTreeView

  • I have a QTreeView in combination with QFileSystemModel, showing the entire content of my computer (all drives on Windows).
    @m_model = new QFileSystemModel(this);
    m_treeView = new QTreeView(this);

    I need to show the tree view expanded to initialPath from the beginning. I tried this:

    But it works only when initialPath is one of the drives (like C:). In that case the drive is shown expanded.
    I also tried this, which doesn't seem to work.
    @auto rootIdx = m_model->setRootPath(initialPath);
    m_treeView->selectionModel()->select(rootIdx, QItemSelectionModel::Select);@

    Any idea?

  • Lifetime Qt Champion


    Since the QFileSystemModel loading is threaded, I would rather call expand in a slot connected to "directoryLoaded":http://doc.qt.io/qt-5/qfilesystemmodel.html#directoryLoaded

    Hope it helps

  • Moderators

    it also ensures that all parent nodes are expanded

  • QTreeView::scrollTo() does the trick!
    directoryLoaded() wasn't helpful.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.