How to customize QTreeWidget header's font

How to customize QTreeWidget header's font

qtreewidget hide header
pyqt css
qtreewidgetitem::setdata
qtreewidget add widget

Using:

tree=QtGui.QTreeWidget()
tree.setHeaderLabels(['Column 1','Column 2','Column 3'])
tree.setColumnWidth(0, 48)  
tree.setColumnWidth(1, 48)  
tree.setColumnWidth(2, 48)  

QTreeWidget is created. Its column headers are given names. And headers horizontal sizes are set.

Now I would like to customize the headers font size.

font = QtGui.QFont()
font.setPointSize(8)
tree.headerItem().setFont(0, font)

But .setFont used in syntax like this does not have any visible effect on font size used in headers.

I've also tried:

tree.headerItem().setFont(0, font)
tree.headerItem().setFont(1, font)
tree.headerItem().setFont(2, font)

with no success. What method to use to customize font size used in QTreeWidget's column headers?

EDITED LATER:

Example 1 makes the column headers larger with no effect on font size (?!):

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
tree=QtGui.QTreeWidget()
tree.setHeaderLabels(['One','Two','Tree','Four','Five'])
font=QtGui.QFont()            
font.setPointSize(24)
tree.header().setFont(font)
tree.show()
sys.exit(app.exec_())

Example 2 tries to use QSS = no effect on font size again:

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
tree=QtGui.QTreeWidget()
tree.setHeaderLabels(['One','Two','Tree','Four','Five'])
tree.header().setStyleSheet('font: bold 24px; font-size: 32pt; font-family: Courier;')
tree.setStyleSheet('font: bold 24px; font-size: 32pt; font-family: Courier;')
tree.show()
sys.exit(app.exec_())

Example 3 uses setHeaderItem. No success:

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
tree=QtGui.QTreeWidget()

font=QtGui.QFont() 
font.setPointSize(36)
item=QtGui.QTreeWidgetItem()
item.setText(0,'Column name')
item.setFont(0,font)
tree.setHeaderItem(item)    
tree.show()
sys.exit(app.exec_())
EXAMPLE 5 via MAIN WINDOW:
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
mainWindow=QtGui.QMainWindow()
mainWidget=QtGui.QWidget()
mainWindow.setCentralWidget(mainWidget)
mainLayout = QtGui.QVBoxLayout()
mainWidget.setLayout(mainLayout)
mainWindow.setStyleSheet('QWidget { font: bold italic large "Times New Roman" }')
tree=QtGui.QTreeWidget()
tree.setHeaderLabels(['One','Two','Tree','Four','Five'])
mainLayout.addWidget(tree)
mainWindow.show()
sys.exit(app.exec_())
EDIT # 2

I've just run these example codes on another machine (OSX). And to my surprise the code is working and the header font is set properly. Interesting what could be a reason it is working on one Mac and doesn't work on another? Could it be a sip issue? Qt Version? PyQt version? Here is the screenshot:


It might be possible to just use QSS for this:

http://doc.qt.io/qt-4.8/stylesheet-examples.html

how to custom Header of QTreeWidget, use setHeaderItem , i use setHeaderItem & setItemWidget, but it didnot work do i have to inherite a child class of QHeaderView, to customize the Header?and if i  For the person in the comments (and basically anyone else stopping by who might be helped by this), here is the translated python version of the code below:


Assuming you want the font to be the same for the entire header, you should be able to do:

tree.header().setFont(font)

QTreeWidget Class, I was able to set the foreground of it but cannot change the background color. Here is QTreeWidgetItem *header = myTree->headerItem(); Now the problem is QTreeWidget is taking full space in vertical direction and then after leaving a very big space after QTreeWidget, although only rows exist currently in it, QLabel is added. I want that second Qlabel is added immediately after two rows visible and now when more rows are added then It shift downwards.


Why can't I set backgroung of a QTreeWidget's header item?, QTreeWidget - Header and Customizing. In this section, we'll learn how to set the headers of the QTreeWidget. Also we'll customize the columns of the tree. The problem for me was that StretchLastSection was set to true and if this value is set to true, this property will override the resize mode set on the last section in the header. Solution: _treeWidget = new UIWidgetSceneTree(); _treeWidget->header()->setStretchLastSection(false); _treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);


Qt5 Tutorial QTreeWidget - 2020, Please refer to our Privacy Policy in case you change your mind, or if you want to learn more about how we take care of your privacy. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to change the header background color of a QTableView


Getting header names from QTreeWidget (Qt), QTreeWidget.setHeaderLabels() , but a custom header can be constructed with a PySide.QtGui.QTreeWidgetItem and inserted into the tree with the PySide. Thanks, I didn't know about that feature of stylesheets. Unfortunately, it still doesn't help me because the QHeaderView only contains a single stylesheet property for setting the background color, and it is applied to all Header sections. I need to be able to change the background color for each section separately.


QTreeWidget, strings with setHeaderLabels(), but a custom header can be constructed with a QTreeWidgetItem and inserted into the tree with the setHeaderItem() function. The tree can have a header that contains a section for each column in the widget. It is easiest to set up the labels for each section by supplying a list of strings with setHeaderLabels (), but a custom header can be constructed with a QTreeWidgetItem and inserted into the tree with the setHeaderItem () function.