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. Sorting QTreeView() columns by clicking on header
Forum Update on Monday, May 27th 2025

Sorting QTreeView() columns by clicking on header

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.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.
  • M Offline
    M Offline
    Mary02
    wrote on 8 Nov 2022, 11:11 last edited by
    #1

    I've used

    treeView->setSortingEnabled(true);
    

    to sort the view by clicking the view's horizontal header and it doesn't work only for last column. When I click the last column there appears sort indicator (when I click its direction change up/down) but the column doesn't sort. The rest of the columns work good.
    What can I do to fix it? I've not found such a problem anywhere.

    J 1 Reply Last reply 8 Nov 2022, 13:19
    0
    • M Mary02
      8 Nov 2022, 11:11

      I've used

      treeView->setSortingEnabled(true);
      

      to sort the view by clicking the view's horizontal header and it doesn't work only for last column. When I click the last column there appears sort indicator (when I click its direction change up/down) but the column doesn't sort. The rest of the columns work good.
      What can I do to fix it? I've not found such a problem anywhere.

      J Online
      J Online
      JonB
      wrote on 8 Nov 2022, 13:19 last edited by JonB 11 Aug 2022, 13:20
      #2

      @Mary02
      If it is only the last column, in case it is relevant what type of data does that hold? And what type of model is it attached to?

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mary02
        wrote on 21 Nov 2022, 09:34 last edited by
        #3

        In last column I hold QMetaType. I created enum class MyType where I assign numbers to value and declare AMetaType MyType. I added also

        inline bool operator<(MyType left, MyType right) {
        return static_cast<std::underlying_type_t<MyType>>(left) < static_cast<std::underlying_type_t<MyType>>(right);
        }
        

        for use in context where the enum needs to be sortable but sorting still doesn't work.

        J 1 Reply Last reply 21 Nov 2022, 09:49
        0
        • M Mary02
          21 Nov 2022, 09:34

          In last column I hold QMetaType. I created enum class MyType where I assign numbers to value and declare AMetaType MyType. I added also

          inline bool operator<(MyType left, MyType right) {
          return static_cast<std::underlying_type_t<MyType>>(left) < static_cast<std::underlying_type_t<MyType>>(right);
          }
          

          for use in context where the enum needs to be sortable but sorting still doesn't work.

          J Online
          J Online
          JonB
          wrote on 21 Nov 2022, 09:49 last edited by
          #4

          @Mary02
          Maybe this was worth mentioning in the first place, since this is the only column you say does not sort? Did you put a qDebug() statement into your method to verify it is getting called?

          M 1 Reply Last reply 21 Nov 2022, 23:30
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 21 Nov 2022, 15:51 last edited by Christian Ehrlicher
            #5

            And how do you use this type?

            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
            • J JonB
              21 Nov 2022, 09:49

              @Mary02
              Maybe this was worth mentioning in the first place, since this is the only column you say does not sort? Did you put a qDebug() statement into your method to verify it is getting called?

              M Offline
              M Offline
              Mary02
              wrote on 21 Nov 2022, 23:30 last edited by
              #6

              @JonB
              I put qDebug() and my method is not even called.

              J 1 Reply Last reply 22 Nov 2022, 07:56
              0
              • M Mary02
                21 Nov 2022, 23:30

                @JonB
                I put qDebug() and my method is not even called.

                J Online
                J Online
                JonB
                wrote on 22 Nov 2022, 07:56 last edited by JonB
                #7

                @Mary02
                That is why I would put a qDebug() in! So now you know why your method has no effect. As @Christian-Ehrlicher asked, you now have to show/explain how/where you use your type and why you think that method should be called during sorting. Qt treats the data in model columns/rows as QVariants, why would it even be interested in/now about your enum type during its sorting?

                M 1 Reply Last reply 22 Nov 2022, 08:48
                0
                • J JonB
                  22 Nov 2022, 07:56

                  @Mary02
                  That is why I would put a qDebug() in! So now you know why your method has no effect. As @Christian-Ehrlicher asked, you now have to show/explain how/where you use your type and why you think that method should be called during sorting. Qt treats the data in model columns/rows as QVariants, why would it even be interested in/now about your enum type during its sorting?

                  M Offline
                  M Offline
                  Mary02
                  wrote on 22 Nov 2022, 08:48 last edited by
                  #8

                  @JonB
                  Ok, I am brand new in Qt/C++. As you mentioned Qt treats data in model as QVariant, I used qDebug() to print the data from the last column, converting it to QVariant, and it turned out that instead of my values, there are empty fields. It's strange because another column that also contains my own variable types displays the correct values after conversion.

                  J 1 Reply Last reply 22 Nov 2022, 09:31
                  0
                  • M Mary02
                    22 Nov 2022, 08:48

                    @JonB
                    Ok, I am brand new in Qt/C++. As you mentioned Qt treats data in model as QVariant, I used qDebug() to print the data from the last column, converting it to QVariant, and it turned out that instead of my values, there are empty fields. It's strange because another column that also contains my own variable types displays the correct values after conversion.

                    J Online
                    J Online
                    JonB
                    wrote on 22 Nov 2022, 09:31 last edited by JonB
                    #9

                    @Mary02 said in Sorting QTreeView() columns by clicking on header:

                    also contains my own variable types displays the correct values after conversion

                    And does that call an inline bool operator<(MyType left, MyType right) method you have written for that type? Or does not, and just happens to output in anticipated order so you would not know?

                    I am brand new in Qt/C++

                    Then what made you write that less-than method for your type, and what makes you think anything will call it?

                    And btw: I would assume that for an enumerated type left to its own devices Qt will treat an enumerated type value as an int and use that for any comparison purposes. Is that not sufficient/working for your case?

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 22 Nov 2022, 15:35 last edited by
                      #10

                      Again: how do you use this enum? Do you have a QStandardItemModel, a custom model derived from QAbstractItemModel or do you use a QTreeWidget instead a QTreeView?

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

                      M 1 Reply Last reply 23 Nov 2022, 07:58
                      0
                      • C Christian Ehrlicher
                        22 Nov 2022, 15:35

                        Again: how do you use this enum? Do you have a QStandardItemModel, a custom model derived from QAbstractItemModel or do you use a QTreeWidget instead a QTreeView?

                        M Offline
                        M Offline
                        Mary02
                        wrote on 23 Nov 2022, 07:58 last edited by
                        #11

                        @Christian-Ehrlicher
                        I use a QTreeView.

                        J 1 Reply Last reply 23 Nov 2022, 08:32
                        0
                        • M Mary02
                          23 Nov 2022, 07:58

                          @Christian-Ehrlicher
                          I use a QTreeView.

                          J Online
                          J Online
                          JonB
                          wrote on 23 Nov 2022, 08:32 last edited by
                          #12

                          @Mary02
                          This does not answer the question. What model do you use, and how do you think your enumerated type/its comparison operator are involved?

                          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