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. Sort Model, Select Multi Header Column !
Forum Updated to NodeBB v4.3 + New Features

Sort Model, Select Multi Header Column !

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 2.8k 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.
  • VRoninV VRonin

    Not on the view but it can be done on the model.

    If your model does not use insert/remove columns you can:

    • use this proxy
    • call proxyModel->addSortPriority to select the order of the multiple sorts
    • call proxyModel->sort(i); (i being any number) to sort
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #3

    @VRonin
    I have 3 observations/questions on your code:

    1. You may or may not care, but for published work I suspect your revoveSortPriority is mis-spelt and shold be removeSortPriority?

    2. I don't think your code allows the sorting to be in a different (default) direction per each column (e.g. my sort order requires column 2 to be descending when my column 2 is sorted ascending, like SQL ORDER BY col1 ASC, col2 DESC)?

    3. I don't understand what the i parameter to the sort is for in this situation?

    For #2 & #3, I may be misunderstanding the OP's question/your code. I thought this is about "how can I sort by multiple columns" (like I believe you can do in Excel?), so for example my column 1 may contain the same values in multiple rows, I then want it sub-sorted by column 2.

    VRoninV 1 Reply Last reply
    1
    • JonBJ JonB

      @VRonin
      I have 3 observations/questions on your code:

      1. You may or may not care, but for published work I suspect your revoveSortPriority is mis-spelt and shold be removeSortPriority?

      2. I don't think your code allows the sorting to be in a different (default) direction per each column (e.g. my sort order requires column 2 to be descending when my column 2 is sorted ascending, like SQL ORDER BY col1 ASC, col2 DESC)?

      3. I don't understand what the i parameter to the sort is for in this situation?

      For #2 & #3, I may be misunderstanding the OP's question/your code. I thought this is about "how can I sort by multiple columns" (like I believe you can do in Excel?), so for example my column 1 may contain the same values in multiple rows, I then want it sub-sorted by column 2.

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #4

      @JonB said in Sort Model, Select Multi Header Column !:

      You may or may not care, but for published work I suspect your revoveSortPriority is mis-spelt and shold be removeSortPriority?

      Yup

      I don't think your code allows the sorting to be in a different (default) direction per each column (e.g. my sort order requires column 2 to be descending when my column 2 is sorted ascending, like SQL ORDER BY col1 ASC, col2 DESC)?

      That's a very good observation, that code was a "quick solution" drafted for another user, I'm planning on making that a "real" thing and I added this to the things to implement: https://github.com/VSRonin/QtModelUtilities/issues/4

      I don't understand what the i parameter to the sort is for in this situation?

      QAbstractItemModel::sort requires a number, the proxy model then just ignores that number so i can be anything, the result won't change

      for example my column 1 may contain the same values in multiple rows, I then want it sub-sorted by column 2

      proxyModel->addSortPriority(1);
      proxyModel->addSortPriority(2);
      proxyModel->sort(0);
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      JonBJ 1 Reply Last reply
      1
      • VRoninV VRonin

        @JonB said in Sort Model, Select Multi Header Column !:

        You may or may not care, but for published work I suspect your revoveSortPriority is mis-spelt and shold be removeSortPriority?

        Yup

        I don't think your code allows the sorting to be in a different (default) direction per each column (e.g. my sort order requires column 2 to be descending when my column 2 is sorted ascending, like SQL ORDER BY col1 ASC, col2 DESC)?

        That's a very good observation, that code was a "quick solution" drafted for another user, I'm planning on making that a "real" thing and I added this to the things to implement: https://github.com/VSRonin/QtModelUtilities/issues/4

        I don't understand what the i parameter to the sort is for in this situation?

        QAbstractItemModel::sort requires a number, the proxy model then just ignores that number so i can be anything, the result won't change

        for example my column 1 may contain the same values in multiple rows, I then want it sub-sorted by column 2

        proxyModel->addSortPriority(1);
        proxyModel->addSortPriority(2);
        proxyModel->sort(0);
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #5

        @VRonin

        QAbstractItemModel::sort requires a number, the proxy model then just ignores that number so i can be anything, the result won't change

        Ah, I didn't understand that is what you meant.

        You shouldn't do it that way, should you [politely]? It feels all wrong to me. If it were me, I think I would:

        1. Provide a new sort() method which takes no column parameter (assuming you can do that in C++, I can't in Python), and that does your sort.

        2. If you have to provide an override for sort(int column), the only one which would make sense to me would go like:

        if (column == m_sortPriority[0].first)
            return sort();
        else
            throw "Bad argument value";
        
        1. Your code allows specification of Qt::ItemDataRole for each function on each individual column in the multi-sort, and stores it in the QPair list element. This seems strange to me. The specification of a role should be shared by the values of all columns in the sort, e.g. I want them all for Qt::DisplayRole or all for Qt::EditRole in a given sort. Then you could drop storing the Qt::ItemDataRole in the QPair and use it to store the "natural direction" for each column's sort order which we discussed above.

        Please don't take any of this as criticism or arrogance! They're just my thoughts for discussion :)

        VRoninV 1 Reply Last reply
        0
        • JonBJ JonB

          @VRonin

          QAbstractItemModel::sort requires a number, the proxy model then just ignores that number so i can be anything, the result won't change

          Ah, I didn't understand that is what you meant.

          You shouldn't do it that way, should you [politely]? It feels all wrong to me. If it were me, I think I would:

          1. Provide a new sort() method which takes no column parameter (assuming you can do that in C++, I can't in Python), and that does your sort.

          2. If you have to provide an override for sort(int column), the only one which would make sense to me would go like:

          if (column == m_sortPriority[0].first)
              return sort();
          else
              throw "Bad argument value";
          
          1. Your code allows specification of Qt::ItemDataRole for each function on each individual column in the multi-sort, and stores it in the QPair list element. This seems strange to me. The specification of a role should be shared by the values of all columns in the sort, e.g. I want them all for Qt::DisplayRole or all for Qt::EditRole in a given sort. Then you could drop storing the Qt::ItemDataRole in the QPair and use it to store the "natural direction" for each column's sort order which we discussed above.

          Please don't take any of this as criticism or arrogance! They're just my thoughts for discussion :)

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #6

          @JonB said in Sort Model, Select Multi Header Column !:

          Provide a new sort() method which takes no column parameter (assuming you can do that in C++

          You can and I agree. The view will still call sort(int) though

          If you have to provide an override for sort(int column), the only one which would make sense to me

          Agree but I'd rather not override it at all, I can't think of a strictly better implementation than that of QSortFilterProxyModel::sort.

          Your code allows specification of Qt::ItemDataRole for each function on each individual column

          I still stick to my implementation. Say you want to sort the first column by the displayed column and the second column by some property you saved in Qt::UserRole

          Please don't take any of this as criticism or arrogance! They're just my thoughts for discussion :)

          Don't worry, I apreciate the discussion and the points you raise are more than reasonable

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          JonBJ 1 Reply Last reply
          2
          • VRoninV VRonin

            @JonB said in Sort Model, Select Multi Header Column !:

            Provide a new sort() method which takes no column parameter (assuming you can do that in C++

            You can and I agree. The view will still call sort(int) though

            If you have to provide an override for sort(int column), the only one which would make sense to me

            Agree but I'd rather not override it at all, I can't think of a strictly better implementation than that of QSortFilterProxyModel::sort.

            Your code allows specification of Qt::ItemDataRole for each function on each individual column

            I still stick to my implementation. Say you want to sort the first column by the displayed column and the second column by some property you saved in Qt::UserRole

            Please don't take any of this as criticism or arrogance! They're just my thoughts for discussion :)

            Don't worry, I apreciate the discussion and the points you raise are more than reasonable

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

            @VRonin said in Sort Model, Select Multi Header Column !:

            I still stick to my implementation. Say you want to sort the first column by the displayed column and the second column by some property you saved in Qt::UserRole

            Then (to me) you are going to have problems deriving from the base QSortFilterProxyModel Class. What are you going to do about properties like http://doc.qt.io/qt-5/qsortfilterproxymodel.html#sortRole-prop ?

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #8

              Exactly the same thing I do with the column argument passed to sort. i ignore it as addSortPriority allows you a more generic solution

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              JonBJ 1 Reply Last reply
              0
              • VRoninV VRonin

                Exactly the same thing I do with the column argument passed to sort. i ignore it as addSortPriority allows you a more generic solution

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

                @VRonin
                Then we just have a difference of opinion on the approach to use for these kinds of methods. Which is fine, as I'm sure you have your reasons :)

                Incidentally, I just also noticed, your class is named MultySortProxy when the usual spelling would be MultiSortProxy. Might be a thought if you ever change revoveSortPriority, though I appreciate it would mean changing the class name.

                At which point, I shall shut up now. It has been interesting looking at your code, thank you.

                1 Reply Last reply
                0
                • VRoninV VRonin

                  Not on the view but it can be done on the model.

                  If your model does not use insert/remove columns you can:

                  • use this proxy
                  • call proxyModel->addSortPriority to select the order of the multiple sorts
                  • call proxyModel->sort(i); (i being any number) to sort
                  P Offline
                  P Offline
                  Pada_
                  wrote on last edited by
                  #10

                  @VRonin

                  In the sorting model, I want to select multiple headers like Excel.

                  In an alignment model, only one header is clicked or dragged.

                  VRoninV 1 Reply Last reply
                  0
                  • P Pada_

                    @VRonin

                    In the sorting model, I want to select multiple headers like Excel.

                    In an alignment model, only one header is clicked or dragged.

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #11

                    @Pada_ said in Sort Model, Select Multi Header Column !:

                    like Excel.

                    Can you explain step-by-step how to obtain what you want in excel, I'll then "translate" it to Qt

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    P 1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @Pada_ said in Sort Model, Select Multi Header Column !:

                      like Excel.

                      Can you explain step-by-step how to obtain what you want in excel, I'll then "translate" it to Qt

                      P Offline
                      P Offline
                      Pada_
                      wrote on last edited by
                      #12

                      @VRonin

                      Like the Excel headers (A, B, C, and D), you want to select multiple header columns in Qt's list model.

                      However, when using the sort model in the Qt list model, a triangle appears next to the header column, and only one header column can be selected.

                      My Qt list model has five header columns. I want to use sort for header columns 1 through 3, and the rest 4 through 5 like to be able to select multiple header columns like a regular list model.

                      JonBJ 1 Reply Last reply
                      0
                      • P Pada_

                        @VRonin

                        Like the Excel headers (A, B, C, and D), you want to select multiple header columns in Qt's list model.

                        However, when using the sort model in the Qt list model, a triangle appears next to the header column, and only one header column can be selected.

                        My Qt list model has five header columns. I want to use sort for header columns 1 through 3, and the rest 4 through 5 like to be able to select multiple header columns like a regular list model.

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

                        @Pada_
                        You have to do this work yourself.

                        Apart from @VRonin's kind suggestion of his own skeleton code, Googling for qtableview sort multiple columns will give you several solutions to look at.

                        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