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. Is it possible to sort on multiple columns in QTableView using QSortFilterProxyModel
QtWS25 Last Chance

Is it possible to sort on multiple columns in QTableView using QSortFilterProxyModel

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 18.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.
  • A Offline
    A Offline
    ashwindas
    wrote on last edited by
    #1

    Is it possible to sort on multiple columns in QTableView using QSortFilterProxyModel? at least programmatically.

    The examples show sorting on one column, which is achieved by enabling sorting on the view and then later, implementing our own lessThan function in QSortFilterProxyModel.
    I want to know if there is a way to achieve multiple column sort using the MVC classes.

    1 Reply Last reply
    0
    • ? This user is from outside of this forum
      ? This user is from outside of this forum
      Guest
      wrote on last edited by
      #2

      AFAIK, you might have to extend QSortFilterProxyModel and build this capability of sorting on multiple columns.

      There is a workaround, but not optimal in terms of performance ...

      If you want to sort by Col A and then by Col B, then first sort by Col B and then sort by Col A... it should work. :)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ashwindas
        wrote on last edited by
        #3

        Thanks chetankjain. I think this works...
        but by default they don't support multiple column sort like other popular frameworks which provide tables :( I think this has to be included in the future.

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #4

          glad it worked, developing this component would be a good exercise :)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            It is not hard to make a generic QSortFilterProxy subclass that can sort on multiple columns and that can be applied generically. If you think it would be useful, please write one and share it, either by trying to merge it into Qt or by just sharing the component via your own website or one of the community sites like qt-apps.org.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maximus
              wrote on last edited by
              #6

              Hi Andre,

              I found this code that is supposed to work for mutliple column filtering.
              http://www.qtcentre.org/threads/24267-QSortFilterProxyModel-setFilterRegExp-for-more-than-1-column
              Maybe it could be integrated in QSortFilterProxyModel?

              Yet i'm still having trouble using this code. i'll try to code my own class so I can understand better why my implementation doesn't work
              Thank you


              Free Indoor Cycling Software - https://maximumtrainer.com

              1 Reply Last reply
              0
              • M Offline
                M Offline
                maximus
                wrote on last edited by
                #7

                I found this line of code was important :
                proxyModel->invalidate();
                every time the filter change, call that :)


                Free Indoor Cycling Software - https://maximumtrainer.com

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  ekoswibowo
                  wrote on last edited by
                  #8

                  Can we do multi column sort using QTableWidget? I would love to keep my custom widget subclassed from it.. I think I will rework a lot of works if I have to change it from QTableView

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    I don't think so, no. You'd have to use a custom QSortFilterProxyModel, but I don't think you can make sure that the QTableWidget's internal model will stay alive once you set another model on it. My personal view is that the widget versions of the views were a mistake to introduce in the first place.

                    I think the easiest way to port is to use QStandardItemModel instead, and use a QTabelView instead of a QTableModel. QStandardItemModel has an API that is very similar to the data related API of QTableWidget, so that should not be too big an issue. You can then use whatever proxy model you need to do your advanced sorting.

                    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