Important: Please read the Qt Code of Conduct -

QTreeView and QFileSystemModel showing dirs only. How to hide expansion marks against the empty dirs?

  • I'm building somewhat like standard file explorer - left pane is for folders tree, and the right one to display files within selected folder.

    QTreeView with QFileSystemModel is used to display folders. Model's filter is set to QDir::Dirs | QDir::NoDotAndDotDot to list dirs only, no files. I want to display expansion marks only against the folders with subfolders, i. e. if some dir is empty or contains only files, it shouldn't be expandable. But instead, tree view keeps expansion marks against the empty dir. And that's the question: how to hide them?

    I've searched the solution here, in the google, in the QT examples - no success. While the question, I think, is very easy to answer. The only solution I came for at the moment is to subclass QAbstractItemModel. That's pain.

    QT 4.8, QT Creator, C++.

    Here's code to demonstrate:

    @#include <QApplication>

    #include <QFileSystemModel>
    #include <QTreeView>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);

    QTreeView w;
    QFileSystemModel m;
    m.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
    return a.exec(&#41;;


  • For what you want you will subclass QFileSystemModel and rewrite the data() method. In addition, to manage expansions marks hide or show you have to rewrite the rowCount() method and filter by parent.

Log in to reply