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 Updated to NodeBB v4.3 + New Features

Sorting Folders First QFileSystemModel With Directory Filter

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.7k 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.
  • E Offline
    E Offline
    EverydayDiesel
    wrote on 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!

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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

        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!

        JonBJ Online
        JonBJ Online
        JonB
        wrote on 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
        • JonBJ JonB referenced this topic on

        • Login

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