Search code examples
c++qtqtreewidget

How to centralize the QTreeWidget header


How to centralize the QTreeWidget header?

I have the following QTreeWidget:

enter image description here

And I want to centralize the header to look like this:

enter image description here

I already tried to set this stylesheet:

QHeaderView::section {
     text-align: center;
 }

But it didn't work.

I know that with QTableWidget we can do something like this:

myTableWidget->horizontalHeaderItem(0)->setTextAlignment(Qt::AlignCenter);

But QTreeWidget doesn't have horizontalHeaderItem().

I'm on Qt 5.3.


Solution

  • I found a solution:

    ui->myTreeWidget->header()->setDefaultAlignment(Qt::AlignCenter);
    

    Set the default alignment as centralized to the QHeaderView.

    Documentation: http://doc.qt.io/qt-4.8/qheaderview.html#defaultAlignment-prop