Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView and QFileSystemModel showing dirs only. How to hide expansion marks against the empty dirs?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 3.3k 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.
  • G Offline
    G Offline
    gumanoid
    wrote on last edited by
    #1

    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);
    m.setRootPath("C:\\");
    
    w.setModel(&m);
    w.setRootIndex(m.index(m.rootPath()));
    w.hideColumn(3);
    w.hideColumn(2);
    w.hideColumn(1);
    
    w.show();
    
    return a.exec(&#41;;
    

    }@

    1 Reply Last reply
    0
    • francescmmF Offline
      francescmmF Offline
      francescmm
      wrote on last edited by
      #2

      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.

      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 Extensions
      • Unsolved