Important: Please read the Qt Code of Conduct -

[Solved] Different headers in QTableViews using same model

  • Hi,
    I have two QTableViews which use the same model (a subclass of QAbstractItemModel). These tables display data in two different ways and I would like them to display two different strings in their horizontal headers. I already override the QAbstractItemModel::headerData method and return a string for the Qt::DisplayRole, but I can´t distinguish which table is requesting it, so until now I always return the same string, which is not optimal.
    I guess a solution might be creating two new roles (something like MyRoles::DisplayRoleForTable1 and MyRoles::DisplayRoleForTable2) and make each table use one of those instead of Qt::DisplayRole, but I am not sure how to achieve this.

    I would be grateful for any help.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You could use a "QIdentityProxyModel": between your views (one per view) and your model to return the string you want.

    Hope it helps

  • Nice and clean solution, thanks a lot!

  • Lifetime Qt Champion

    You're welcome !

    Don't forget to update the thread's title prepending solved so other forum users may know that a solution has been found :)

Log in to reply