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 Updated to NodeBB v4.3 + New Features

Sorting QTreeView() columns by clicking on header

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.6k 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 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.

    JonBJ 1 Reply Last reply
    0
    • M Mary02

      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.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on 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
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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
        • JonBJ JonB

          @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 last edited by
          #6

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

          JonBJ 1 Reply Last reply
          0
          • M Mary02

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

            JonBJ Online
            JonBJ Online
            JonB
            wrote on 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
            0
            • JonBJ JonB

              @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 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.

              JonBJ 1 Reply Last reply
              0
              • M Mary02

                @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.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on 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
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    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 last edited by
                    #11

                    @Christian-Ehrlicher
                    I use a QTreeView.

                    JonBJ 1 Reply Last reply
                    0
                    • M Mary02

                      @Christian-Ehrlicher
                      I use a QTreeView.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on 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