I'm developing a computer application with PySide and I'm using the QTableWidget. Let's say my table has 3 columns, but the data they contain is very different, like (for each row) a long sentence in the first column, then 3-digit numbers in the two last columns. I'd like to have my table resize in order to adjust its size to the data, or at least to be able to set the column sizes as (say) 70/15/15 % of the available space.
What is the best way to do this ?
I've tried table.horizontalHeader().setResizeMode(QHeaderView.Stretch)
after reading this question but it makes 3 columns of the same size.
I've also tried table.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
thanks to Fabio's comment but it doesn't fill all the available space as needed.
Neither Interactive
, Fixed
, Stretch
, ResizeToContents
from the QHeaderView documentation seem 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 kind of a workaround but it's still not what I'm looking for :
header = table.horizontalHeader() header.setResizeMode(QHeaderView.ResizeToContents) header.setStretchLastSection(True)
It would be better if there existed a setStretchFirstSection
method, but unfortunately there does not seem to be one.
EDIT 2 :
The only thing that can be modified in the table is the last column, the user can enter a number in it. Red arrows indicates what I'd like to have.
Here's what happens with Stretch
Here's what happens with ResizeToContents
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With