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: