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. QTreeView with QFileSystemModel not sorting
Forum Updated to NodeBB v4.3 + New Features

QTreeView with QFileSystemModel not sorting

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.8k 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.
  • DiracsbracketD Offline
    DiracsbracketD Offline
    Diracsbracket
    wrote on last edited by Diracsbracket
    #1

    Hi,
    I have a QTreeView for a QFileSystemModel in which I only keep the Name column visible.

    I have set

    m_dirTree->setSortingEnabled(true);
    m_dirTree->sortByColumn(index.column(), Qt::AscendingOrder);
    

    and use the following to add a subfolder to the currently selected folder:

    index = m_treeModel->mkdir(index, foldername);
    

    The view is updated to show the newly added folder, but it is appended at the end of the subdir list. So even if I name my new folder AA, it will still show after ZZZ.

    Forcing a sort by clicking on the header's sort arrow does not correct this either?
    What is going on?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      QObject::connect(m_treeModel,&QAbstractItemModel::dataChanged,[=]()->void {m_treeModel->sort(0);});

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      DiracsbracketD 1 Reply Last reply
      2
      • VRoninV VRonin

        QObject::connect(m_treeModel,&QAbstractItemModel::dataChanged,[=]()->void {m_treeModel->sort(0);});

        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on last edited by Diracsbracket
        #3

        @VRonin said in QTreeView with QFileSystemModel not sorting:

        QObject::connect(m_treeModel,&QAbstractItemModel::dataChanged,=->void {m_treeModel->sort(0);});

        Thanks @VRonin .

        I tried you connect, and the slot is called, but the sort has no effect.

        The thing is, when I create the AA folder manually in Explorer, the tree updates correctly and shows AA at the top. So maybe I should try using another method to make the directory than

        QFileSysModel::mkdir()
        

        Indeed, when I use QDir::mkdirI() the model updates to the correct sort order...

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Wish You Happiness
          wrote on last edited by
          #4

          Coincidentally, I had the same problem as you before.

          Define that: m_model is the object of class QFileSystemModel, and m_rootpath is the root path of your filetree.

          In the constructor function of QTreeView, there should be the following code:

          m_model->setRootPath(m_rootpath);
          setModel(m_model);
          setRootIndex(m_model->index(m_rootpath));
          

          Add the following code where you have done the file operation.

          m_model->setRootPath("");
          m_model->setRootPath(m_rootpath);
          

          The method WORKS in my project !

          1 Reply Last reply
          0

          • Login

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