PySide - PyQt: How do I make the width of a QTableWidget column equal to the proportion of available space?

I am developing a computer application with PySide, and I am using QTableWidget. Say my table has 3 columns, but the data they contain is very different, for example (for each row) a long sentence in the first column, and then 3-digit numbers in the last two columns. I would like to resize the table to adjust its size to the data , or at least to set the column sizes as (say) 70/15/15% of the available space .

What is the best way to do this?

I tried table.horizontalHeader().setResizeMode(QHeaderView.Stretch) after reading this question , but it makes 3 columns of the same size.

I also tried table.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents) thanks to Fabio comment , but it doesn't fill all available space as needed.

Neither Interactive , Fixed , Stretch , ResizeToContents from the ResizeToContents documentation seems to give me what I need (see second edit).

Any help would be appreciated, even if it is for Qt / C ++! Thank you very much.


EDIT: I found a workaround, but it's still not the one I'm looking for:

 header = table.horizontalHeader() header.setResizeMode(QHeaderView.ResizeToContents) header.setStretchLastSection(True) 

It would be better if the setStretchFirstSection method setStretchFirstSection , but, unfortunately, it looks like it is not alone.


EDIT 2:

The only thing that can be changed in the table is the last column, the user can enter a number in it. Red arrows indicate what I would like to have.

Here's what happens with Stretch Stretch

Here's what happens with ResizeToContents ResizeToContents

+5
source share
3 answers

This can be solved by setting the resize mode for each column. The first section should stretch to take up free space, while the last two sections simply 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) 
+12
source

You can do this with QItemDelegates or QStyledItemDelegates . If you want to resize to content and you have automatic stretching, you will need to select the “stretch” column.

 class ResizeDelegate(QStyledItemDelegate): def __init__(self, table, stretch_column, *args, **kwargs): super(ResizeDelegate, self).__init__(*args, **kwargs) self.table = table self.stretch_column = stretch_column def sizeHint(self, option, index): size = super(ResizeDelegate, self).sizeHint(option, index) if index.column() == self.stretch_column: total_width = self.table.viewport().size().width() calc_width = size.width() for i in range(self.table.columnCount()): if i != index.column(): option_ = QtGui.QStyleOptionViewItem() index_ = self.table.model().index(index.row(), i) self.initStyleOption(option_, index_) size_ = self.sizeHint(option_, index_) calc_width += size_.width() if calc_width < total_width: size.setWidth(size.width() + total_width - calc_width) return size ... table = QTableWidget() delegate = ResizeDelegate(table, 0) table.setItemDelegate(delegate) ... # Add items to table table.resizeColumnsToContents() 

You can set ResizeToContents sizing ResizeToContents , or if you want the user to adjust the width of the column as needed, just call resizeColumnsToContents manually after making changes to the table elements.

You may also need to slightly calculate the width calculations due to the margins and the filling between the columns (for example, add a pixel or two to calculated_width for each column to take into account the cell border).

+2
source

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) 
+2
source

All Articles