Solved QTreeView header text alignment
-
Hello everybody!
I'm facing this strange problem: as you see in the image below, I'm working on a tree view and I cannot set the proper alignment on the header text. (On Qt 5.15.2)
I am using:
ui->treeView->header()->setDefaultAlignment(Qt::AlignBottom | Qt::AlignRight);
I am also increasing the default height of the lines and this is showing the issue clearer.
You can find my full code here: https://gitlab.com/lknth/myFileManagerHow can I fix this?
Thanks in advance for your help!
-
The view's default alignment is only used if the model doesn't provide alignment for the header.
You need to implement theQt::TextAlignmentRole
in yourheaderData()
to either return the alignment you want or an emptyQVariant
, in which case the view's default will be usedQVariant MyFileSystemProxyModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role == Qt::TextAlignmentRole) return QVariant(); return QSortFilterProxyModel::headerData(section, orientation, role); }
-
@Chris-Kawa said in QTreeView header text alignment:
QVariant MyFileSystemProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::TextAlignmentRole)
return QVariant();return QSortFilterProxyModel::headerData(section, orientation, role);
}
Thank you very much! It works!