Is automatic (re)sizing of QTableWidget possible?
-
I cannot make QTableWidget automatically size or resize itself. Here is a fragment of code I use:
@QWidget* w = new QWidget();
w->setLayout(new QVBoxLayout());
// ...
// Here I create another widget called 'plot'
// ...
w->layout()->addWidget(plot);
// Add inner horizontal layout to vertical layout of widget 'w'
QHBoxLayout* l = new QHBoxLayout();
static_cast<QVBoxLayout*>(w->layout())->addLayout(l);
// Prepare the table
QTableWidget* table = new QTableWidget(4, 6, this);
table->verticalHeader()->setVisible(false);
table->horizontalHeader()->setVisible(false);
// ...
// here I fill the table (all 6x4 cells)
// ...
l->addWidget(table); l->addStretch();
ui->tabWidget->addTab(w, "Test");
@I want the following. The tab widget (created in designer) has the central widget 'w', which has a vertical outer layout (containing the 'plot' widget and an inner horizontal layout). The inner layout contains QTableWidget 'table' and a spacer. The table should resize itself to occupy minimal, but sufficient space to show all its rows and columns (the tab is large enough to allow this). However, the height of the table is always larger than necessary (there is enough space for a couple of additional rows) and the width is either larger or smaller than necessary (if it is smaller, a horizontal scrollbar appears, although the tab width is much larger than the width of all columns combined).
Calling table->resizeColumnsToContents() and table->resizeRowsToContents() does not help. Setting size policy of the table (to any size policy) does not help either. Is there a way to automatically (re)size QTableWidget?
-
If using spacers the layout of the widget will work with the resizeToColumn functions etc.
-
What kind of spacers is needed for these functions to work? I already have a stretchable space added to the inner horizontal layout, it does not help. It forces the table to occupy exactly one half of the tab width (or some other fixed amount of space if I set stretching factors manually). And if I add a stretchable space to the outer vertical layout, this leads to shrinking of the plot widget, not the table.
-
Hi,
It is very hard to tell what to do without a picture or layout setup.
Does your TabWidget tabs contain a layout? Otherwise the tab will just hold a widget (which does have a layout and will resize if the widget is changed) that will not resize with the resize of the tabwidget. -
Just use. Any kind of layout. put table widget in that. It will resize automatically.
-
All right, I have tried to picture it:
!http://i62.tinypic.com/r8s5xz.jpg(Layouts)!
Widget 'w', created at runtime, is inserted as a new tab into tab widget (ui->tabWidget->addTab(w, "Test")). The tab widget itself is inserted into a vertical layout and resizes itself properly. Widget 'w' has a vertical layout with the 'plot' widget and a horizontal layout inserted into it. The plot widget also has no problems with resizing. Horizontal layout has table widget and a stretchable space. The table widget resizes with the window, but its initial size is always larger than its contents (both vertical and horizontal dimensions, if I do not specify stretching factors). I have also tried to make an additional inner layout for the table only:@QHBoxLayout* ll = new QHBoxLayout();
l->addLayout(ll);
ll->addWidget(table); l->addStretch();@It has no effect.