Stretch QTableWidget columns.
-
I have a question on how to resize a QTableWidget's header and columns. The table is part of a QWidget that is using a grid layout. I have been able to get the column widths to resize using resizeColumnsToContents(), and the table itself does adjust when you adjust the window, but the headers don't adjust. I tried using horizontalHeader().setSectionResizeMode(qtw.QHeaderView.Stretch), which does adjust the header sizes, but the contents in the columns themselves are squashed. The only way I could get everything to resize correctly was to maximize the window. I don't want the window to be maximized. So is there a way to both size the columns with their contents, and get the headers to also adjust? Or should I just get the size of the QTableWidget after it's been populated, and then resize the window's width based on that?
-
Good morning @Jason-Olson,
I am not sure if I understand the difference between
a) resizing column width (-> resized to fit contents)
b) adjusting headers (their width always corresponds to the column width)
With the table widget placed in a grid layout, the available space and its resize behavior can be controlled from the grid layout.
Without seeing any code, it's hard to guess how this can be done.
Check here and here for more information about (grid) layouts.
Cheers
Axel -
I'm going to attach some pictures so I can better explain what is going on.
In this image you can see that the columns have been resized to fit the contents in them, but you'll notice that the headers didn't stretch or adjust their size. If you resize the window the following happens.
Now if you tell the headers to stretch, the following happens.
The headers stretch, and the window does resize correctly, but the contents of the columns does not resize.
What I'm asking is how to get the columns to size based on their contents, and allow the headers to stretch as well.
-
@Jason-Olson said in Stretch QTableWidget columns.:
horizontalHeader().setSectionResizeMode(qtw.QHeaderView.Stretch)
can you try?
horizontalHeader().setSectionResizeMode(qtw.QHeaderView.ResizeToContents) -
@Jason-Olson I think the problem is caused by global font size change. You may set your own font size for this specific widget. The font size has to be adjusted when the widget is resized.
for your first picture, you may add
m_tableWidget->horizontalHeader()->setStretchLastSection( true );
to stretch the last column only in case of not fitting well.