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
Forum Updated to NodeBB v4.3 + New Features

QSortFilterProxyModel will filter but not sort

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.0k Views 1 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.
  • O Offline
    O Offline
    ocgltd
    wrote on 12 Aug 2021, 14:19 last edited by ocgltd 8 Dec 2021, 14:53
    #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)

    K 1 Reply Last reply 12 Aug 2021, 14:59
    0
    • O ocgltd
      12 Aug 2021, 14:19

      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)

      K Offline
      K Offline
      KroMignon
      wrote on 12 Aug 2021, 14:59 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)

      O 1 Reply Last reply 12 Aug 2021, 15:22
      1
      • K KroMignon
        12 Aug 2021, 14:59

        @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)

        O Offline
        O Offline
        ocgltd
        wrote on 12 Aug 2021, 15:22 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)

        K 1 Reply Last reply 12 Aug 2021, 21:14
        0
        • O ocgltd
          12 Aug 2021, 15:22

          @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)

          K Offline
          K Offline
          KroMignon
          wrote on 12 Aug 2021, 21:14 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)

          J 1 Reply Last reply 13 Aug 2021, 06:10
          0
          • K KroMignon
            12 Aug 2021, 21:14

            @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.

            J Offline
            J Offline
            JonB
            wrote on 13 Aug 2021, 06:10 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.

            O 1 Reply Last reply 13 Aug 2021, 18:23
            0
            • J JonB
              13 Aug 2021, 06:10

              @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.

              O Offline
              O Offline
              ocgltd
              wrote on 13 Aug 2021, 18:23 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.

              J 1 Reply Last reply 13 Aug 2021, 19:16
              0
              • O ocgltd
                13 Aug 2021, 18:23

                @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.

                J Offline
                J Offline
                JonB
                wrote on 13 Aug 2021, 19:16 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

                1/7

                12 Aug 2021, 14:19

                • Login

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