Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

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

    General and Desktop
    4
    9
    16953
    Loading More Posts
    • 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
      ashwindas last edited by

      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 Reply Quote 0
      • ?
        Guest last edited by

        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 Reply Quote 0
        • A
          ashwindas last edited by

          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 Reply Quote 0
          • ?
            Guest last edited by

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

            1 Reply Last reply Reply Quote 0
            • A
              andre last edited by

              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 Reply Quote 0
              • M
                maximus last edited by

                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 Reply Quote 0
                • M
                  maximus last edited by

                  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 Reply Quote 0
                  • E
                    ekoswibowo last edited by

                    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 Reply Quote 0
                    • A
                      andre last edited by

                      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 Reply Quote 0
                      • First post
                        Last post