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 files by name: QFileSystemModel vs QSortFilterProxyModel
Forum Updated to NodeBB v4.3 + New Features

Sorting files by name: QFileSystemModel vs QSortFilterProxyModel

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.7k Views 2 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.
  • myUsernameM Offline
    myUsernameM Offline
    myUsername
    wrote on last edited by
    #1

    Hello everybody!

    I'm pretty new to Qt and I'm facing this strange problem: as you see in the image below, I'm working on 2 tree views, the one on the left contains the local files and it's based on QFileSystemModel, while the one on the right contains remote files and it's based on QSortFilterProxyModel.

    As you can see, the have the same files, both sorted by name, but they have a different order.
    Seems like the QFileSystemModel isn't sorting well, what could be the reason?
    Have you ever noticed that? Is it a "feature" of Qt or a bug in my code?

    a1ddeb5c-76ba-45c8-a789-d76bf3f7ca6d-image.png

    If it's something of Qt, what would be the best workaround to fix that?

    Thanks in advance for your help!
    [and happy new year!]

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QFileSystemModel is using QCollator with numericMode on, QSortFilterProxyModel simply sorts the strings without any special stuff

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      myUsernameM 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        QFileSystemModel is using QCollator with numericMode on, QSortFilterProxyModel simply sorts the strings without any special stuff

        myUsernameM Offline
        myUsernameM Offline
        myUsername
        wrote on last edited by
        #3

        @Christian-Ehrlicher thanks a lot for the quick feedback!
        Is it possible to easily "disable" that? Actually I prefer the other way of sorting.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @myUsername said in Sorting files by name: QFileSystemModel vs QSortFilterProxyModel:

          Is it possible to easily "disable" that? Actually I prefer the other way of sorting.

          No, but why can't you simply use a QSortFilterProxyModel?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          myUsernameM 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @myUsername said in Sorting files by name: QFileSystemModel vs QSortFilterProxyModel:

            Is it possible to easily "disable" that? Actually I prefer the other way of sorting.

            No, but why can't you simply use a QSortFilterProxyModel?

            myUsernameM Offline
            myUsernameM Offline
            myUsername
            wrote on last edited by
            #5

            @Christian-Ehrlicher thanks for the clear answer.
            I'm going to try to re-implement that using QSortFilterProxyModel.
            I was just hoping for an easier solution😅

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @myUsername said in Sorting files by name: QFileSystemModel vs QSortFilterProxyModel:

              I'm going to try to re-implement that using QSortFilterProxyModel.

              What do you need to reimplement here? Simply set it as proxy model and you're fine.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              myUsernameM 1 Reply Last reply
              2
              • rrlopezR Offline
                rrlopezR Offline
                rrlopez
                wrote on last edited by
                #7

                In order to use QFileSystemModel, you can as well re-implement the "sort" function since the base implementation does nothing:
                https://doc.qt.io/qt-5/qfilesystemmodel.html#sort

                If you want to sort like the one in the right, I would recommend using the QSortFilterProxyModel as is. If you want something more customizable then re-implementing the QFileSystemModel would be a better option.

                Lic-Ing. Rodrigo Lopez Gonzalez
                Embedded Software Engineer
                RidgeRun Engineering Ltd.
                www.ridgerun.com
                Email: rodrigo.lopez@ridgerun.com

                1 Reply Last reply
                3
                • Christian EhrlicherC Christian Ehrlicher

                  @myUsername said in Sorting files by name: QFileSystemModel vs QSortFilterProxyModel:

                  I'm going to try to re-implement that using QSortFilterProxyModel.

                  What do you need to reimplement here? Simply set it as proxy model and you're fine.

                  myUsernameM Offline
                  myUsernameM Offline
                  myUsername
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher Do you mean to use QFileSystemModel as source model for QSortFilterProxyModel?
                  I would like to be able to use methods, like setRootPath, setFilter, setNameFilters...

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by SGaist
                    #9

                    Hi,

                    You still can.

                    It just sits between the view and the other model.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    myUsernameM 1 Reply Last reply
                    3
                    • SGaistS SGaist

                      Hi,

                      You still can.

                      It just sits between the view and the other model.

                      myUsernameM Offline
                      myUsernameM Offline
                      myUsername
                      wrote on last edited by
                      #10

                      Thanks to everybody for the help!
                      I think I understood what to do.
                      I just found out the methods mapFromSource and mapToSource, which should allow me to easily switch between the 2 models.

                      1 Reply Last reply
                      2

                      • Login

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