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. Always display the visual index number of a QHeaderView after sorting
QtWS25 Last Chance

Always display the visual index number of a QHeaderView after sorting

Scheduled Pinned Locked Moved Solved General and Desktop
qheaderviewqtableview
5 Posts 3 Posters 3.3k 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.
  • P Offline
    P Offline
    panosk
    wrote on last edited by
    #1

    Is it possible to always display the visual index instead of the logical index so the header names show row numbers in ascending order even after sorting a model?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      AFAIK, you'll have to implement that yourself.

      What kind of model are you using ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        panosk
        wrote on last edited by
        #3

        @SGaist
        Thanks for you reply.
        I'm using a QAbstractTableModel so I'm already implementing the headerData(int, Qt::Orientation, int) method and return header values for the Qt::Horizontal orientation. I don't know though if this is the right place to make this work, e.g for the Qt::Vertical orientation. The data structure of the model is a QMap<int,QSqlRecord*> so the entries are automatically sorted in ascending order. Then I pass this model to a QSortFilterProxyModel and sort in descending order before I display the entries in a QTableView.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thEClaw
          wrote on last edited by
          #4

          You might want to reimplement QSortFilterProxyModel::headerData to just return something like "visualIndex()" or "row()", without actually querying the source model. I am not sure if one could consider that a clean solution, but I think it is at least a very simple one.

          P 1 Reply Last reply
          0
          • T thEClaw

            You might want to reimplement QSortFilterProxyModel::headerData to just return something like "visualIndex()" or "row()", without actually querying the source model. I am not sure if one could consider that a clean solution, but I think it is at least a very simple one.

            P Offline
            P Offline
            panosk
            wrote on last edited by panosk
            #5

            @thEClaw
            Well, since you were worried about how clean your solution was, I thought I should come up with a nastier hack: -): I'm storing the ints in my map as negative numbers and then I retrieve the "real" numbers in the model's data() method like this:

            ...
            if (item.column() == 2) {
                    int fake = recordsMap.keys().at(item.row());
                    return abs(fake);
            }
            ...
            

            At least everything works and displays fine now.

            I fiddled a lot with visualIndex() and logicalIndex() but couldn't find a way to make this work, so I'm keeping this for the time being. Thanks anyway for your suggestion!

            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