Center aligns content of cells in QTableView

I have a QTableView.

Is there a way to center align the entire cell content of this view?

I am not using a delegate. This is just the one AbstractTableModel

that is added as a model to QTableView

.

How should I align the content of each cell in the center?

Thank.

+3


source to share


1 answer


If you don't want to use custom delegates, you can set this in data

your model's implementation function 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);
}

      

+7


source







All Articles