Always display the visual index number of a QHeaderView after sorting



  • 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?


  • Lifetime Qt Champion

    Hi,

    AFAIK, you'll have to implement that yourself.

    What kind of model are you using ?



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



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



  • @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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.