Is there anyway to let QTableWidget's header items stretch to full size just like QTreeWidget does ?
Asked
Active
Viewed 1.8k times
1 Answers
22
From the QTableView
documentation:
By default, the cells in a table do not expand to fill the available space.
You can make the cells fill the available space by stretching the last header section. Access the relevant header using
horizontalHeader()
orverticalHeader()
and set the header'sstretchLastSection
property.
You should give that a try.
QTableWidget *tw = ...;
tw->horizontalHeader()->setStretchLastSection(true);
The stretchLastSection
documentation has:
Note: The horizontal headers provided by QTreeView are configured with this property set to true, ensuring that the view does not waste any of the space assigned to it for its header.
So that's how the tree views do it.
-
This holds true for `QTableView`, not for `QTableWidget`. – SexyBeast Nov 13 '14 at 01:35
-
@Cupidvogel: could you elaborate? QTableWidgets are QTableViews. – Mat Nov 13 '14 at 05:38
-
No, the methods you mentioned, like `horizontalHeader` or `verticalHeader` don't belong to `QTableWidget`, they belong to `QTableView`. Plus the method don't work as expected in `QTableView` as well, now. I will be posting a question on this shortly. – SexyBeast Nov 13 '14 at 09:30
-
QTableWidget extends QTableView, whatever methods the view has, the widget has. – Mat Nov 13 '14 at 09:33
-
Oh, now it seems to be working fine. Looks like I was missing a forward declaration. But it doesn't work any way.. – SexyBeast Nov 13 '14 at 09:35
-
You can also check `horizontalHeaderStretchLastSection` if you're using Qt Designer – Claudiu Aug 20 '15 at 22:35