Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QFileSystemModel + QTreeView doesn't sort



  • Hello,
    i used the Qt example dirview to get an idea to use QFileSystemModel.
    c:\Qt\Examples\Qt-5.12.1\widgets\itemviews\dirview\

    I created a new QWidgets application and add a QTreeView to the mainwindow.ui.
    Thats all i changed in the ui.

    add in mainwindow.h

    QFileSystemModel *model;
    

    and my mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
      , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        model = new QFileSystemModel(this);
        model->setRootPath( QString("C:/") );
        model->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
        ui->treeView->setModel(model);
    
        // Settings TreeView
        ui->treeView->setAnimated(false);
        ui->treeView->setIndentation(20);
        ui->treeView->setSortingEnabled(true);
        ui->treeView->sortByColumn(0, Qt::AscendingOrder);
        ui->treeView->setWindowTitle(QObject::tr("Explorer"));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    The code is nearly the same as from the example.
    BUT the example is sorting all columns, my code not.
    I disabled all code lines from the example that belong to QCommandLineParser
    and QModelIndex, in that case it is nearly the same code as my own code and the example sorts (!!).
    I have no idea why not. Can anyone help?
    Regards



  • For me it sorts 2nd+ level folders, but not the top level ones that are drives.
    The "dirview" example sorts everything.

    (edit)
    changed the rootPath from "C:/" to "", now it sorts everything.


Log in to reply