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. QFileSystemModel + QTreeView doesn't sort
QtWS25 Last Chance

QFileSystemModel + QTreeView doesn't sort

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 498 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.
  • R Offline
    R Offline
    RoadCoder
    wrote on last edited by RoadCoder
    #1

    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

    1 Reply Last reply
    0
    • ZueukZ Offline
      ZueukZ Offline
      Zueuk
      wrote on last edited by Zueuk
      #2

      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.

      1 Reply Last reply
      1

      • Login

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