Sorting Folders First QFileSystemModel With Directory Filter
EverydayDiesel last edited by
I am trying to show folders and files inside of a QTreeView.
- Sort by folders first then files
- 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!
JonB last edited by JonB
QDir::DirsFirstis to do with
QDir. There is no "presupplied" option for this for
QFileSystemModel. You need to write code implementing
QSortFilterProxyModel. See examples:
They are old, but I believe they are still relevant.