Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center align the contents of cells in QTableView

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.

like image 769
user1173240 Avatar asked Feb 07 '13 13:02

user1173240


1 Answers

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);
}
like image 151
Nemanja Boric Avatar answered Sep 19 '22 21:09

Nemanja Boric