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. implementing sorting with QSortFilterProxyModel
QtWS25 Last Chance

implementing sorting with QSortFilterProxyModel

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 5 Posters 3.5k 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by mzimmers
    #1

    Hi all -

    I've implemented a QSortFilterProxyModel (on a QAbstractListModel) for the purpose of filtering, which it does just fine. Now, I want to implement a sort of the filtered data. It's not clear to me from the docs how I cause this to happen. The docs state:
    Custom sorting behavior is achieved by subclassing QSortFilterProxyModel and reimplementing lessThan(), which is used to compare items.
    I've done this, but my lessThan() is never called.

    What do I need to do in order to "activate" sorting? From the examples, it doesn't appear that I should actually invoke the sort() method directly.

    EDIT:

    Here's the signatures of my reimplmented functions:

    protected:
        bool filterAcceptsRow(int sourceRow, 
                              const QModelIndex &sourceParent) const override;
        bool lessThan(const QModelIndex &left,
                      const QModelIndex &right) const override;
    

    Thanks...

    Christian EhrlicherC Ronel_qtmasterR 2 Replies Last reply
    0
    • mzimmersM mzimmers

      @Ronel_qtmaster said in implementing sorting with QSortFilterProxyModel:

      you should consider porting your model to qml

      I assume you mean making my model available to QML? I've done that. I've gone through (most of) the steps in the video from the link you provided.

      If you mean actually convert my model to a QML ListModel, that's not an option. My model code is ~1000 lines of C++ and fairly intricate; it's not conducive to conversion to QML.

      What I'm doing (with the connection) seems to be working. In the debugger, it's alarmingly slow, but when run normally, performance seems OK. Unless there's a serious flaw in this approach, I think it's what I have to do.

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #24

      @mzimmers If your model is to be consumed in QML, it most likely has a single column and multiple roles if it's meant to be consumed by a ListView (multiple columns shouldn't be a thing unless you are implementing a spreadsheet, change my mind).

      call setDynamicSortFilter(true) and sort(0) once at the beginning and you should be good to go. Your lessThan function will be called when needed. Or use my project on GitHub like mentioned above. It hasn't been updated for a while but it is still relevant. Take a look at the available forks too, there might be improvements there.

      mzimmersM 1 Reply Last reply
      2
      • mzimmersM mzimmers

        Hi all -

        I've implemented a QSortFilterProxyModel (on a QAbstractListModel) for the purpose of filtering, which it does just fine. Now, I want to implement a sort of the filtered data. It's not clear to me from the docs how I cause this to happen. The docs state:
        Custom sorting behavior is achieved by subclassing QSortFilterProxyModel and reimplementing lessThan(), which is used to compare items.
        I've done this, but my lessThan() is never called.

        What do I need to do in order to "activate" sorting? From the examples, it doesn't appear that I should actually invoke the sort() method directly.

        EDIT:

        Here's the signatures of my reimplmented functions:

        protected:
            bool filterAcceptsRow(int sourceRow, 
                                  const QModelIndex &sourceParent) const override;
            bool lessThan(const QModelIndex &left,
                          const QModelIndex &right) const override;
        

        Thanks...

        Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @mzimmers said in implementing sorting with QSortFilterProxyModel:

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

        If you want to sort a container you have to provide a compare operator. For QSortFilterProxyModel you have to provide the comparision through the lessThan() function

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

        mzimmersM 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @mzimmers said in implementing sorting with QSortFilterProxyModel:

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

          If you want to sort a container you have to provide a compare operator. For QSortFilterProxyModel you have to provide the comparision through the lessThan() function

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #3

          @Christian-Ehrlicher right -- my lessThan() function is currently empty (save for a qDebug() statement) but it should still be called, no?

          Christian EhrlicherC 1 Reply Last reply
          0
          • mzimmersM mzimmers

            @Christian-Ehrlicher right -- my lessThan() function is currently empty (save for a qDebug() statement) but it should still be called, no?

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @mzimmers said in implementing sorting with QSortFilterProxyModel:

            but it should still be called, no?

            It will be called as soon as you sort your view.

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

            mzimmersM 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @mzimmers said in implementing sorting with QSortFilterProxyModel:

              but it should still be called, no?

              It will be called as soon as you sort your view.

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #5

              @Christian-Ehrlicher ahh...and, since (according to the docs) QAbstractItemModel::sort() does nothing by default, I guess I do have to implement my own sort algorithm?

              Christian EhrlicherC 1 Reply Last reply
              0
              • mzimmersM mzimmers

                @Christian-Ehrlicher ahh...and, since (according to the docs) QAbstractItemModel::sort() does nothing by default, I guess I do have to implement my own sort algorithm?

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @mzimmers said in implementing sorting with QSortFilterProxyModel:

                I guess I do have to implement my own sort algorithm?

                As already written you have to reimplement QSFPM::lessThan() if you want a custom sorting of your data.

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

                mzimmersM 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @mzimmers said in implementing sorting with QSortFilterProxyModel:

                  I guess I do have to implement my own sort algorithm?

                  As already written you have to reimplement QSFPM::lessThan() if you want a custom sorting of your data.

                  mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #7

                  @Christian-Ehrlicher I realize that. What I'm trying to understand is whether I need to re-implement sort() as well.

                  From the docs:
                  Sorts the model by column in the given order.
                  The base class implementation does nothing.

                  The Qt-supplied examples don't seem to directly invoke a sort() function, and the docs say that it's called behind the scenes. So, I'm still not sure what all I need to supply in order to effect a sort. I do have my lessThan() function defined and I'll flesh it out once I figure out how it gets invoked.

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • mzimmersM mzimmers

                    @Christian-Ehrlicher I realize that. What I'm trying to understand is whether I need to re-implement sort() as well.

                    From the docs:
                    Sorts the model by column in the given order.
                    The base class implementation does nothing.

                    The Qt-supplied examples don't seem to directly invoke a sort() function, and the docs say that it's called behind the scenes. So, I'm still not sure what all I need to supply in order to effect a sort. I do have my lessThan() function defined and I'll flesh it out once I figure out how it gets invoked.

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @mzimmers said in implementing sorting with QSortFilterProxyModel:

                    What I'm trying to understand is whether I need to re-implement sort() as well.

                    If you don't want to use a QSortFilterProxyModel then yes.

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

                    mzimmersM 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @mzimmers said in implementing sorting with QSortFilterProxyModel:

                      What I'm trying to understand is whether I need to re-implement sort() as well.

                      If you don't want to use a QSortFilterProxyModel then yes.

                      mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #9

                      @Christian-Ehrlicher we seem to be talking past each other...either that, or I'm especially dense this morning.

                      As stated in my first post, I do have a QSortFilterProxyModel set onto a QAbstractListModel. Here's the complete header:

                      class EquipmentValveProxy : public QSortFilterProxyModel
                      {
                          Q_OBJECT
                      public:
                          explicit EquipmentValveProxy(QObject *parent = nullptr);
                      protected:
                          bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
                          // void sort(int column, Qt::SortOrder = Qt::AscendingOrder) const;
                          bool lessThan(const QModelIndex &left,
                                        const QModelIndex &right) const override;
                      };
                      

                      When I try to use QSortFilterProxyModel::sort(), I get this error:

                      equipmentValveProxy.cpp:22:5: 'this' argument to member function 'sort' has type 'const EquipmentValveProxy', but function is not marked const
                      qsortfilterproxymodel.h:157:10: 'sort' declared here
                      

                      Am I supposed to implement my own sort() as a wrapper to QSortFilterProxyModel::sort()?

                      Thanks...

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • mzimmersM mzimmers

                        @Christian-Ehrlicher we seem to be talking past each other...either that, or I'm especially dense this morning.

                        As stated in my first post, I do have a QSortFilterProxyModel set onto a QAbstractListModel. Here's the complete header:

                        class EquipmentValveProxy : public QSortFilterProxyModel
                        {
                            Q_OBJECT
                        public:
                            explicit EquipmentValveProxy(QObject *parent = nullptr);
                        protected:
                            bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
                            // void sort(int column, Qt::SortOrder = Qt::AscendingOrder) const;
                            bool lessThan(const QModelIndex &left,
                                          const QModelIndex &right) const override;
                        };
                        

                        When I try to use QSortFilterProxyModel::sort(), I get this error:

                        equipmentValveProxy.cpp:22:5: 'this' argument to member function 'sort' has type 'const EquipmentValveProxy', but function is not marked const
                        qsortfilterproxymodel.h:157:10: 'sort' declared here
                        

                        Am I supposed to implement my own sort() as a wrapper to QSortFilterProxyModel::sort()?

                        Thanks...

                        Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @mzimmers said in implementing sorting with QSortFilterProxyModel:

                        Am I supposed to implement my own sort() as a wrapper to QSortFilterProxyModel::sort()?

                        Why?
                        Again: reimplement lessThan() and then sort your view. Nothing more.

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

                        mzimmersM 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @mzimmers said in implementing sorting with QSortFilterProxyModel:

                          Am I supposed to implement my own sort() as a wrapper to QSortFilterProxyModel::sort()?

                          Why?
                          Again: reimplement lessThan() and then sort your view. Nothing more.

                          mzimmersM Offline
                          mzimmersM Offline
                          mzimmers
                          wrote on last edited by
                          #11

                          @Christian-Ehrlicher

                          I have re-implemented lessThan(). It's currently a stub, but I'd expect it would still get called, if I can figure out how to invoke a sort.

                          But my attempts to use QSortFilterProxyModel::sort() produce a the above compile-time error.

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • mzimmersM mzimmers

                            @Christian-Ehrlicher

                            I have re-implemented lessThan(). It's currently a stub, but I'd expect it would still get called, if I can figure out how to invoke a sort.

                            But my attempts to use QSortFilterProxyModel::sort() produce a the above compile-time error.

                            Christian EhrlicherC Online
                            Christian EhrlicherC Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @mzimmers said in implementing sorting with QSortFilterProxyModel:

                            , if I can figure out how to invoke a sort.

                            already told you: It will be called as soon as you sort your view.

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

                            mzimmersM 1 Reply Last reply
                            1
                            • Christian EhrlicherC Christian Ehrlicher

                              @mzimmers said in implementing sorting with QSortFilterProxyModel:

                              , if I can figure out how to invoke a sort.

                              already told you: It will be called as soon as you sort your view.

                              mzimmersM Offline
                              mzimmersM Offline
                              mzimmers
                              wrote on last edited by
                              #13

                              @Christian-Ehrlicher ahhhhhh...thank you for the emphasis; now I understand what you're saying. But...I don't know how to do that.

                              My view is a QML ListView. How does one effect a sort on that?

                              Christian EhrlicherC JonBJ 2 Replies Last reply
                              0
                              • mzimmersM mzimmers

                                @Christian-Ehrlicher ahhhhhh...thank you for the emphasis; now I understand what you're saying. But...I don't know how to do that.

                                My view is a QML ListView. How does one effect a sort on that?

                                Christian EhrlicherC Online
                                Christian EhrlicherC Online
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by Christian Ehrlicher
                                #14

                                I don't use QML but with QList/Table/TreeView the sorting is done by clicking on the header item.

                                You can call QSFPM::sort() from c++ but don't know how/if it is possible directly in the ListView

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

                                1 Reply Last reply
                                0
                                • mzimmersM mzimmers

                                  @Christian-Ehrlicher ahhhhhh...thank you for the emphasis; now I understand what you're saying. But...I don't know how to do that.

                                  My view is a QML ListView. How does one effect a sort on that?

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by JonB
                                  #15

                                  @mzimmers
                                  I would just start by Googling QML QSortFilterProxyModel. https://stackoverflow.com/questions/45563393/sort-the-data-of-a-qml-listview-by-using-a-qsortfilterproxymodel might be just what you need to understand from QML (though it might be too detailed), I don't know. Note that it talks about implementing the lessThan(). That is all you have to do for a QSortFilterProxyModel. Its own code will call that repeatedly in order to present its source model in sorted order to the outside world.

                                  1 Reply Last reply
                                  1
                                  • mzimmersM mzimmers

                                    Hi all -

                                    I've implemented a QSortFilterProxyModel (on a QAbstractListModel) for the purpose of filtering, which it does just fine. Now, I want to implement a sort of the filtered data. It's not clear to me from the docs how I cause this to happen. The docs state:
                                    Custom sorting behavior is achieved by subclassing QSortFilterProxyModel and reimplementing lessThan(), which is used to compare items.
                                    I've done this, but my lessThan() is never called.

                                    What do I need to do in order to "activate" sorting? From the examples, it doesn't appear that I should actually invoke the sort() method directly.

                                    EDIT:

                                    Here's the signatures of my reimplmented functions:

                                    protected:
                                        bool filterAcceptsRow(int sourceRow, 
                                                              const QModelIndex &sourceParent) const override;
                                        bool lessThan(const QModelIndex &left,
                                                      const QModelIndex &right) const override;
                                    

                                    Thanks...

                                    Ronel_qtmasterR Offline
                                    Ronel_qtmasterR Offline
                                    Ronel_qtmaster
                                    wrote on last edited by Ronel_qtmaster
                                    #16

                                    @mzimmers Here is an exemple.Hope it helps
                                    SortOnSecondModel.h

                                    class SortOnSecondModel : public QSortFilterProxyModel
                                    {
                                    public:
                                    SortOnSecondModel( QObject *parent = 0 );
                                    protected:
                                    bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
                                    };

                                    SortOnSecondModel.cpp

                                    bool SortOnSecondModel::lessThan( const QModelIndex &left,
                                    const QModelIndex &right ) const
                                    {
                                    QString leftString = sourceModel()->data( left ).toString();
                                    QString rightString = sourceModel()->data( right ).toString();
                                    if( !leftString.isEmpty() )
                                    leftString = leftString.mid( 1 );
                                    if( !rightString.isEmpty() )
                                    rightString = rightstring.mid( 1 );
                                    return leftString < rightString;
                                    }

                                    mzimmersM 1 Reply Last reply
                                    1
                                    • Ronel_qtmasterR Ronel_qtmaster

                                      @mzimmers Here is an exemple.Hope it helps
                                      SortOnSecondModel.h

                                      class SortOnSecondModel : public QSortFilterProxyModel
                                      {
                                      public:
                                      SortOnSecondModel( QObject *parent = 0 );
                                      protected:
                                      bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
                                      };

                                      SortOnSecondModel.cpp

                                      bool SortOnSecondModel::lessThan( const QModelIndex &left,
                                      const QModelIndex &right ) const
                                      {
                                      QString leftString = sourceModel()->data( left ).toString();
                                      QString rightString = sourceModel()->data( right ).toString();
                                      if( !leftString.isEmpty() )
                                      leftString = leftString.mid( 1 );
                                      if( !rightString.isEmpty() )
                                      rightString = rightstring.mid( 1 );
                                      return leftString < rightString;
                                      }

                                      mzimmersM Offline
                                      mzimmersM Offline
                                      mzimmers
                                      wrote on last edited by
                                      #17

                                      @Ronel_qtmaster thanks for the example. I haven't gotten around to implementing my lessThan() function, because I'm still trying to figure out how/where to invoke the sort() call. This is what I'm trying:

                                      EquipmentValveProxy::EquipmentValveProxy(EquipmentModel *equipmentModel, QObject *parent)
                                          : QSortFilterProxyModel{parent}, m_equipmentModel(equipmentModel)
                                      {
                                          QObject::connect(m_equipmentModel, &EquipmentModel::modelUpdated, this, &EquipmentValveProxy::updateSorting);
                                      }
                                      
                                      void EquipmentValveProxy::updateSorting()
                                      {
                                          invalidate();
                                          sort(0); // what should I use for a column?
                                      }
                                      

                                      This is successful in that it gets me into my lessThan() function, currently stubbed as:

                                      bool EquipmentValveProxy::lessThan(const QModelIndex &left, const QModelIndex &right) const
                                      {
                                          bool rc = false;
                                      
                                          QVariant leftData = sourceModel()->data(left);
                                          QVariant rightData = sourceModel()->data(right);
                                          return rc;
                                      }
                                      

                                      Unfortunately, I'm still doing something wrong, because both leftData and rightData are invalid after assignments. How does this look to you?

                                      Thanks...

                                      Ronel_qtmasterR JonBJ 2 Replies Last reply
                                      0
                                      • mzimmersM mzimmers

                                        @Ronel_qtmaster thanks for the example. I haven't gotten around to implementing my lessThan() function, because I'm still trying to figure out how/where to invoke the sort() call. This is what I'm trying:

                                        EquipmentValveProxy::EquipmentValveProxy(EquipmentModel *equipmentModel, QObject *parent)
                                            : QSortFilterProxyModel{parent}, m_equipmentModel(equipmentModel)
                                        {
                                            QObject::connect(m_equipmentModel, &EquipmentModel::modelUpdated, this, &EquipmentValveProxy::updateSorting);
                                        }
                                        
                                        void EquipmentValveProxy::updateSorting()
                                        {
                                            invalidate();
                                            sort(0); // what should I use for a column?
                                        }
                                        

                                        This is successful in that it gets me into my lessThan() function, currently stubbed as:

                                        bool EquipmentValveProxy::lessThan(const QModelIndex &left, const QModelIndex &right) const
                                        {
                                            bool rc = false;
                                        
                                            QVariant leftData = sourceModel()->data(left);
                                            QVariant rightData = sourceModel()->data(right);
                                            return rc;
                                        }
                                        

                                        Unfortunately, I'm still doing something wrong, because both leftData and rightData are invalid after assignments. How does this look to you?

                                        Thanks...

                                        Ronel_qtmasterR Offline
                                        Ronel_qtmasterR Offline
                                        Ronel_qtmaster
                                        wrote on last edited by
                                        #18

                                        @mzimmers said in implementing sorting with QSortFilterProxyModel:

                                        To invoque the sort , you just need to call QTableView::setSortingEnabled( true );

                                        In your EquipmentValveProxy class, you don't need any connect in the constructor, you only need to reimplement lessThan function.

                                        Now you need to pass the model containing your data to the sorter class.
                                        EquipmentValveProxy ::setSourceModel( &model );

                                        Then set the sorter class as model to your QTableView
                                        QTableView::setModel(&your_sorter_class);

                                        And finally enable sorting using QTableView::setSortingEnabled( true );

                                        mzimmersM 1 Reply Last reply
                                        1
                                        • Ronel_qtmasterR Ronel_qtmaster

                                          @mzimmers said in implementing sorting with QSortFilterProxyModel:

                                          To invoque the sort , you just need to call QTableView::setSortingEnabled( true );

                                          In your EquipmentValveProxy class, you don't need any connect in the constructor, you only need to reimplement lessThan function.

                                          Now you need to pass the model containing your data to the sorter class.
                                          EquipmentValveProxy ::setSourceModel( &model );

                                          Then set the sorter class as model to your QTableView
                                          QTableView::setModel(&your_sorter_class);

                                          And finally enable sorting using QTableView::setSortingEnabled( true );

                                          mzimmersM Offline
                                          mzimmersM Offline
                                          mzimmers
                                          wrote on last edited by
                                          #19

                                          @Ronel_qtmaster said in implementing sorting with QSortFilterProxyModel:

                                          To invoque the sort , you just need to call QTableView::setSortingEnabled( true );

                                          But...it's a QML ListView, not a C++ TableView.

                                          Ronel_qtmasterR 1 Reply Last reply
                                          1
                                          • mzimmersM mzimmers

                                            @Ronel_qtmaster said in implementing sorting with QSortFilterProxyModel:

                                            To invoque the sort , you just need to call QTableView::setSortingEnabled( true );

                                            But...it's a QML ListView, not a C++ TableView.

                                            Ronel_qtmasterR Offline
                                            Ronel_qtmasterR Offline
                                            Ronel_qtmaster
                                            wrote on last edited by
                                            #20

                                            @mzimmers Okay.Then you should consider porting your model to qml https://doc.qt.io/qt-6/qtquick-modelviewsdata-cppmodels.html

                                            mzimmersM 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