1

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: enter image description here enter image description here

alphanumeric
  • 17,967
  • 64
  • 244
  • 392

3 Answers3

2

QTreeWidget column header fonts absolutely are adjustable.

For those arriving here from the distant dystopian future, the sanest way to do so is by styling the QHeaderView::section selector in the stylesheet associated with the QMainWindow widget for your application. This approach sanely centralizes all styles into a single stylesheet and enables instant preview of those styles in Qt (Creator|Designer).

...wut?

Specifically, assuming Qt (Creator|Designer):

  • Right-click on the QMainWindow widget in the Object tree view for your application.
  • Left-click on the Change styleSheet... menu item.
  • Copy and paste the following QSS:

    QTreeWidget QHeaderView::section {
        font-size: 8pt;
    }
    
  • Click OK.

Voilà! Qt stylesheets (QSS) are uncannily powerful. All too often, they're the only means of accomplishing a given aesthetic task in a cross-platform and forward-compatible manner. ...which kind of sucks, actually.

Note lastly that the QTreeWidget parent selector above is technically optional in this case, but permits a single tree widget rather than all tree widgets to be styled (e.g., QTreeWidget#muh_widget QHeaderView::section, styling the column headers for a single tree widget with the object name muh_widget).

Tell Me More, Good Sir!

Gladly, kind gentleman. See also:

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Cecil Curry
  • 9,789
  • 5
  • 38
  • 52
0

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

tree.header().setFont(font)
Becca codes
  • 542
  • 1
  • 4
  • 14
  • I tried `tree.header().setFont(font)` too. Doesn't work. – alphanumeric Jul 29 '14 at 21:27
  • Interesting, it works on my system. What version of Qt are you using? What operating system? – Becca codes Jul 29 '14 at 21:51
  • Platform: OSX. QtCore.PYQT_VERSION_STR: Version: 4.10.4-snapshot-315a27c0f0a7 – alphanumeric Jul 29 '14 at 21:56
  • Oh, I'm on Windows 7, so I can't really see what you are seeing. Also, I'm sure you've tried this, but have you tried setting the point size to something really large, like 20? On my setup, 8 looks about the same as the default size. (Sorry if it's a dumb suggestion. Just like to rule out all possibilities.) – Becca codes Jul 29 '14 at 22:02
  • I've just posted three examples. None of them works. I have reached the end of internet googling for the answer. Probably the header's font is not customizable. – alphanumeric Jul 31 '14 at 21:35
  • I've just posted EXAMPLE 5 (in my original question). Please take a look. It doesn't work for me.... But thanks for trying! – alphanumeric Jul 31 '14 at 22:14
  • Sorry nothing is working for you. Here is my last guess. Try this line: `QtGui.QApplication.setDesktopSettingsAware(false)` The documentation says you must put it before creating the QApplication object. If it works, you may be affected by: [QTBUG-5469](https://bugreports.qt-project.org/browse/QTBUG-5469). Even if this makes the font work, though, it could cause other problems with your app. – Becca codes Jul 31 '14 at 22:33
  • Unfortunately `QtGui.QApplication.setDesktopSettingsAware(False)` made no difference. It is time to admit `QTreeWidget`'s columnt header's font is not adjustable. – alphanumeric Jul 31 '14 at 23:09
  • **`QTreeWidget` column header fonts *absolutely* are adjustable.** For those arriving here from the distant future, the sanest way to do so is via the ``QHeaderView::section`` stylesheet selector. See [this relevant StackOverflow answer](https://stackoverflow.com/a/11456379/2809027) and [this relevant Qt forum post](https://forum.qt.io/post/125570) for implementation details. – Cecil Curry Oct 05 '18 at 04:33
0

It might be possible to just use QSS for this:

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

jesterjunk
  • 2,342
  • 22
  • 18
synthesizerpatel
  • 27,321
  • 5
  • 74
  • 91
  • Could you post an example on how to use QSS to change the header font properties? – alphanumeric Jul 31 '14 at 21:17
  • Try QWidget { font: bold italic large "Times New Roman" } - verify if that changes the header font. If it does, then you just need to find the correct widget name. I remember there being some hunting for some of the Qt widgets because they didn't have the name you'd expect. You could also use obj.setName("some-name"); in the code and then use the QSS of #some-name { font .. } (just like CSS) – synthesizerpatel Jul 31 '14 at 21:25
  • Like so: `tree.header().setStyleSheet('QWidget { font: bold italic large "Times New Roman" }')` ? – alphanumeric Jul 31 '14 at 21:31
  • I'd actually set the MainWindow (since QWidget is the base of all QT widgets, it should cascade down to all widgets (including the header)). Some things can't be modified by QSS. Other things cannot be modified on the fly by QSS and instead require you to initialize the QSS against the application when it starts.. It's a great idea but because of some of the behaviors it has it can be clunky to use.. Hopefully it'll work! – synthesizerpatel Jul 31 '14 at 21:33
  • This doesn't work: `tree.header().setStyleSheet('QMainWindow { font: bold italic large "Times New Roman" }')` – alphanumeric Jul 31 '14 at 21:44
  • No no... don't set the tree's stylesheet - set the MainWindow's stylesheet and make sure it's 'QWidget { font: ..', the point of CSS (and QSS) is the *cascading* aspect. By setting a wide-scope setting like that at the tip-top of your widget tree, it cascades down to all children. – synthesizerpatel Jul 31 '14 at 21:48