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. QSortFilterProxyModel will filter but not sort

QSortFilterProxyModel will filter but not sort

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.9k 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.
  • ocgltdO Offline
    ocgltdO Offline
    ocgltd
    wrote on last edited by ocgltd
    #1

    I have successfully implemented QSortFilterProxyModel and the data is being filtered in my view as soon as my filter criteria change (and I call invalidate). I am now trying to implement the sort functionality, but it's not working. The lessThan function is never being called (I added a qDebug statement to confirm).

    My method is declared like this:

    bool lessThan(const QModelIndex &left,const QModelIndex &right) const override;
    

    Is there some property I must set or method I must call to tell the proxy to do sorting? Do I have to call sort at some point? (Though the column parameters makes no sense given I'm doing a custom sort)

    KroMignonK 1 Reply Last reply
    0
    • ocgltdO ocgltd

      I have successfully implemented QSortFilterProxyModel and the data is being filtered in my view as soon as my filter criteria change (and I call invalidate). I am now trying to implement the sort functionality, but it's not working. The lessThan function is never being called (I added a qDebug statement to confirm).

      My method is declared like this:

      bool lessThan(const QModelIndex &left,const QModelIndex &right) const override;
      

      Is there some property I must set or method I must call to tell the proxy to do sorting? Do I have to call sort at some point? (Though the column parameters makes no sense given I'm doing a custom sort)

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @ocgltd said in QSortFilterProxyModel will filter but not sort:

      Is there some property I must set or method I must call to tell the proxy to do sorting? Do I have to call sort at some point? (Though the column parameters makes no sense given I'm doing a custom sort)

      Yes, at least you have to start a first sorting by calling sort(0, Qt::DescendingOrder) or sort(0, Qt::AscendingOrder)

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      ocgltdO 1 Reply Last reply
      1
      • KroMignonK KroMignon

        @ocgltd said in QSortFilterProxyModel will filter but not sort:

        Is there some property I must set or method I must call to tell the proxy to do sorting? Do I have to call sort at some point? (Though the column parameters makes no sense given I'm doing a custom sort)

        Yes, at least you have to start a first sorting by calling sort(0, Qt::DescendingOrder) or sort(0, Qt::AscendingOrder)

        ocgltdO Offline
        ocgltdO Offline
        ocgltd
        wrote on last edited by
        #3

        @KroMignon When should I call that? In the QSortFilterProxyModel constructor?

        Do I also need to call it every time the filter changes? (Or on some signal)

        KroMignonK 1 Reply Last reply
        0
        • ocgltdO ocgltd

          @KroMignon When should I call that? In the QSortFilterProxyModel constructor?

          Do I also need to call it every time the filter changes? (Or on some signal)

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @ocgltd said in QSortFilterProxyModel will filter but not sort:

          When should I call that? In the QSortFilterProxyModel constructor?
          Do I also need to call it every time the filter changes? (Or on some signal)

          I do it after setting up source model.
          Per default, dynamicSortFilter() is true, so on each model change sort() will be triggered automatically, but the first sort have to be triggered manually.

          This is my experience with QSortFilterProxyModel.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          JonBJ 1 Reply Last reply
          0
          • KroMignonK KroMignon

            @ocgltd said in QSortFilterProxyModel will filter but not sort:

            When should I call that? In the QSortFilterProxyModel constructor?
            Do I also need to call it every time the filter changes? (Or on some signal)

            I do it after setting up source model.
            Per default, dynamicSortFilter() is true, so on each model change sort() will be triggered automatically, but the first sort have to be triggered manually.

            This is my experience with QSortFilterProxyModel.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @KroMignon said in QSortFilterProxyModel will filter but not sort:

            Yes, at least you have to start a first sorting by calling sort(0, Qt::DescendingOrder) or sort(0, Qt::AscendingOrder)

            but the first sort have to be triggered manually.

            I use QSortFilterProxyModel and have never called any sort(), I don't know why you (or @ocgltd?) say you have to? The proxy will sort automatically when first attached. There is a Basic Sort/Filter Model Example which illustrates the minimum needed, and it does not sort().

            @ocgltd
            Whenever you do something like call setFilter...() to change the filter the QSortFilterProxyModel will invalidate and re-sort itself automatically.

            ocgltdO 1 Reply Last reply
            0
            • JonBJ JonB

              @KroMignon said in QSortFilterProxyModel will filter but not sort:

              Yes, at least you have to start a first sorting by calling sort(0, Qt::DescendingOrder) or sort(0, Qt::AscendingOrder)

              but the first sort have to be triggered manually.

              I use QSortFilterProxyModel and have never called any sort(), I don't know why you (or @ocgltd?) say you have to? The proxy will sort automatically when first attached. There is a Basic Sort/Filter Model Example which illustrates the minimum needed, and it does not sort().

              @ocgltd
              Whenever you do something like call setFilter...() to change the filter the QSortFilterProxyModel will invalidate and re-sort itself automatically.

              ocgltdO Offline
              ocgltdO Offline
              ocgltd
              wrote on last edited by
              #6

              @JonB My experience is very different than yours. Changing filters (invalidFilter) does not trigger a sort - in fact it never sorts unless I manually trigger at least one sort. So I call sort in the proxy constructor and now it works.

              JonBJ 1 Reply Last reply
              0
              • ocgltdO ocgltd

                @JonB My experience is very different than yours. Changing filters (invalidFilter) does not trigger a sort - in fact it never sorts unless I manually trigger at least one sort. So I call sort in the proxy constructor and now it works.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @ocgltd , @KroMignon
                Upon review, that sample code does go proxyView->sortByColumn(1, Qt::AscendingOrder);. I searched it, and my code, for sort(. So maybe that is in my code and would explain.

                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