PySide - PyQt : How to make set QTableWidget column width as proportion of the available space? PySide - PyQt : How to make set QTableWidget column width as proportion of the available space? python python

PySide - PyQt : How to make set QTableWidget column width as proportion of the available space?


This can be solved by setting the resize-mode for each column. The first section must stretch to take up the available space, whilst the last two sections just resize to their contents:

PyQt4:

header = self.table.horizontalHeader()header.setResizeMode(0, QtGui.QHeaderView.Stretch)header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)

PyQt5:

header = self.table.horizontalHeader()       header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)


As mentioned before, you can do this by setting the resize-mode of each column. However, if you have a lot of columns this can be a lot of code. The way I do it is setting the "general" resize-mode to "ResizeToContent" and than for one (or more) columns to "Stretch"!

Here is the code:

PyQt4:

header = self.table.horizontalHeader()header.setResizeMode(QtGui.QHeaderView.ResizeToContents)header.setResizeMode(0, QtGui.QHeaderView.Stretch)

PyQt5:

header = self.table.horizontalHeader()header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)       header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)


PyQt4

header = self.table.horizontalHeader()header.setResizeMode(0, QtGui.QHeaderView.Stretch)header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)header.setResizeMode(3, QtGui.QHeaderView.Stretch)

PyQt5

header = self.table.horizontalHeader()       header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)header.setSectionResizeMode(3, QtWidgets.QHeaderView.Stretch)