2

How to change QTableWidget header's font and its content margin and spacing? I would like to make the font for "Column 0", "Column 1" smaller and have no spacing between the name of the columns and the header edge.

enter image description here

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

columns = ['Column 0', 'Column 1', 'Column 2']
items = [['Row%s Col%s'%(row,col) for col in range(len(columns))] for row in range(100)]

view = QtGui.QTableWidget()
view.setColumnCount(len(columns))
view.setHorizontalHeaderLabels(columns)
view.setRowCount(len(items))   
for row, item in enumerate(items):
    for col, column_name in enumerate(item):
        item = QtGui.QTableWidgetItem("%s"%column_name)
        view.setItem(row, col, item)            
    view.setRowHeight(row, 16)

view.show()
app.exec_()
alphanumeric
  • 17,967
  • 64
  • 244
  • 392
  • Are you saying that you want the column width to match the width of the header text, even if the contents of the column is wider? Because in your screenshot, the gap at the top of the column is larger than it is at the bottom (and if you use a smaller font, the difference is going to be even greater). Also, do you really mean *no space at all* between the edge of the header and its text? Surely there needs to be *some* padding there. – ekhumoro Jan 16 '17 at 19:55
  • Sure, I would like to now how to control the padding we see from the edge of the column-header to the header's font itself (the red arrows). I also would like to know how I could control the column-header's font's size and other column-header's font's properties. – alphanumeric Jan 17 '17 at 15:41

2 Answers2

5

I cannot find a way to erase the margins but i can suggest a temporary workaround. You can try to resizeColumnsToContents() before you fill the table with items

For the fonts you can try to do the next

afont = PyQt4.QtGui.QFont()
afont.setFamily("Arial Black")
afont.setPointSize(11)
atable.horizontalHeaderItem(0).setFont(afont)

If you want to see more families, you can always look at the available ones from QtDesigner.

The header items are nothing more than QTableWidgetItems. So all you have to do is get access to them and treat them as any QTableWidgetItem

The answear is almost same with the previous one though.

dterpas
  • 161
  • 1
  • 1
  • 8
4

You can change the fontsize with:

item = QtGui.QTableWidgetItem()
font = QtGui.QFont()
font.setPointSize(14)
item.setFont(font)

I am not sure how to change margin and spacing. I can update this answer if I find out. I suggest using QTDesigner to deal with the layout.

Edit: In QtDesigner you can change the horizontal and vertical header size with the options: horizontalHeaderDefaultSectionSize and verticalHeaderDefaultSectionSize as well as the header font by doble clicking on it and selecting the font you want in the properties. QtDesigner

Diego
  • 1,232
  • 17
  • 20
  • We need to change the font used by `QTableWidget.horizontalHeader()` and not the `QTableWidgetItem.font()` – alphanumeric Jan 15 '17 at 20:57
  • I have managed to do it in QtDesigner. However, when I exported the code the parameters were not in the .py file. – Diego Jan 15 '17 at 21:12