8

Is there anyway to let QTableWidget's header items stretch to full size just like QTreeWidget does ?

daisy
  • 22,498
  • 29
  • 129
  • 265

1 Answers1

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() or verticalHeader() and set the header's stretchLastSection 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.

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 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