Center align the contents of cells in QTableView
Asked Answered
R

1

5

I've a QTableView.

Is there a way to align to the centre, all the cell contents of this view?

I'm not using any delegate. It's just an AbstractTableModel which is added as a model to a QTableView.

How should I align each cell content to the centre?

Thanks.

Rayraya answered 7/2, 2013 at 13:46 Comment(0)
S
9

If you don't want to use custom delegates, you can set this in data function of your model implementation, using Qt::TextAlignmentRole:

QVariant MyModel::data ( const QModelIndex & index, int role = Qt::DisplayRole )
{
         if (role == Qt::TextAlignmentRole )
             return Qt::AlignCenter;
         else
             return QAbstractItemModel::data(index, role);
}
Sloe answered 7/2, 2013 at 14:4 Comment(1)
Qt::AlignCenter is defined as Qt::AlignHCenter | Qt::AlignVCenter . The second one is not needed.Barthold

© 2022 - 2024 — McMap. All rights reserved.