Alignment of Table Horizontal Headers

  • Hello,
    I would like to set the alignment for the horizontal header differently for each column, but it may not be the same as the items in their corresponding column. Help?

  • Just return the proper alignment from your QAbstractItemModel::headerData implementation for the TextAlignmentRole.

  • Being a Qt newbie, could you point me in the correct direction of some sample code to review? Thanks!

  • You can read the docu of QAbstractItemModel::headerData. Afaik, there is no official example, doing this.

  • Moderators

    you could use something like :
    QVariant TableModel::headerData ( int section, Qt::Orientation orientation, int role ) const
    ...other stuff
    if ( orientation == Qt::Horizontal )

    if(role == Qt::TextAlignmentRole ){

               if(col == 1){
                  return Qt::AlignRight;
                  return Qt::AlignLeft;

    return QVariant();

    hope this gives you some directions

  • @ShawnG:

    Could you tell us what model and what view you are using? Are you using a QStandardItemModel with a QTableView, or a QTableWidget? Or something else?

Log in to reply

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