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. Sorting Folders First QFileSystemModel With Directory Filter
Forum Update on Monday, May 27th 2025

Sorting Folders First QFileSystemModel With Directory Filter

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.4k Views
  • 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.
  • E Offline
    E Offline
    EverydayDiesel
    wrote on 31 Mar 2020, 20:47 last edited by
    #1

    Hello,

    I am trying to show folders and files inside of a QTreeView.

    1. Sort by folders first then files
    2. Dont display the root directory or files (It should start showing files in sBasePath)

    I have tried several things but

    QString sBasePath = "/home/myuser/Desktop/displayRoot/subfolder/";
        filesModel = new QFileSystemModel(this);
        filesModel->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot); 
        ui->treeView->setModel(filesModel);
        ui->treeView->setRootIndex(filesModel->setRootPath(sBasePath));
        //ui->treeView->setSorting(QDir::DirsFirst | QDir::IgnoreCase);  // doesnt work
        ui->treeView->setAnimated(false);
        ui->treeView->setIndentation(20);
        ui->treeView->setSortingEnabled(true);
        ui->treeView->hideColumn(1);
        ui->treeView->hideColumn(2);
        ui->treeView->hideColumn(3);
    ui->treeView->setModel(filesModel);
    

    This code works and only display files and folders from "/home/myuser/Desktop/displayRoot" but I need the folders to display first.

    The setSorting method is seem to be what I need but its part of the previous/depreciated version.

    How can I get this to sort correctly?

    Thanks in advance for any help you can give me!

    J 1 Reply Last reply 31 Mar 2020, 21:07
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 31 Mar 2020, 21:06 last edited by
      #2

      Hi,

      You can create your own QAbstractProxyModel that will do the filter or use QSortFilterProxyModel.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • E EverydayDiesel
        31 Mar 2020, 20:47

        Hello,

        I am trying to show folders and files inside of a QTreeView.

        1. Sort by folders first then files
        2. Dont display the root directory or files (It should start showing files in sBasePath)

        I have tried several things but

        QString sBasePath = "/home/myuser/Desktop/displayRoot/subfolder/";
            filesModel = new QFileSystemModel(this);
            filesModel->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot); 
            ui->treeView->setModel(filesModel);
            ui->treeView->setRootIndex(filesModel->setRootPath(sBasePath));
            //ui->treeView->setSorting(QDir::DirsFirst | QDir::IgnoreCase);  // doesnt work
            ui->treeView->setAnimated(false);
            ui->treeView->setIndentation(20);
            ui->treeView->setSortingEnabled(true);
            ui->treeView->hideColumn(1);
            ui->treeView->hideColumn(2);
            ui->treeView->hideColumn(3);
        ui->treeView->setModel(filesModel);
        

        This code works and only display files and folders from "/home/myuser/Desktop/displayRoot" but I need the folders to display first.

        The setSorting method is seem to be what I need but its part of the previous/depreciated version.

        How can I get this to sort correctly?

        Thanks in advance for any help you can give me!

        J Offline
        J Offline
        JonB
        wrote on 31 Mar 2020, 21:07 last edited by JonB
        #3

        @EverydayDiesel
        I think QDir::DirsFirst is to do with QDir. There is no "presupplied" option for this for QFileSystemModel. You need to write code implementing QSortFilterProxyModel. See examples:
        https://stackoverflow.com/questions/10789284/qfilesystemmodel-sorting-dirsfirst
        https://stackoverflow.com/questions/20006775/how-to-sort-folders-above-files-with-qsortfilterproxymodel

        They are old, but I believe they are still relevant.

        1 Reply Last reply
        2
        • J JonB referenced this topic on 27 Mar 2025, 13:56

        1/3

        31 Mar 2020, 20:47

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved