Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Set alignment for QTreeView cells
LeeHyunKyu last edited by
QTreeView Cell Alignment
my_model ->item(row, column)->setTextAlignment(Qt::AlignCenter);
Please suggest me if there is another way.
Why do you want another way?
Denni 0 Banned last edited by
Well not if you want to set the alignment of a single cell but if you want to set the alignment for an entire row or column you can do that as well in one command
I don't know if that is a question. The way Qt works is that alignments etc. are on a cell-by-cell basis, not a complete row/column. That's just how it is. However, using Qt
data()way of doing things, among the roles (https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum) there is e.g.
Qt::TextAlignmentRole, so one can write code to affect all cells in a row/column via that return result instead of actually setting alignment on individual cells.
Denni 0 Banned last edited by Denni 0
Okay yeah I believe with the QTreeView you have to do something like this -- but I know you can affect an entire column and/or row within a QTreeView as I have done it but cannot find it now
class TreeViewModel(QAbstractItemModel): def __init__(self): QAbstractItemModel.__init__(self) def data(self, index, role): if (role == QtCore.Qt.TextAlignmentRole): return QtCore.Qt.AlignHCenter
It is just not very easy to find the how to part
but I know you can affect an entire column and/or row within a QTreeView as I have done it but cannot find it now
You would look at
index.column()to affect row/column:
def data(self, index, role): if (role == QtCore.Qt.TextAlignmentRole): if index.row() == whatever or index.column() == something: return QtCore.Qt.AlignHCenter
You can use a QIdentityProxyModel between your orignal data and your view. You can alter there just what you want.