Thanks for the offer, but after some research, I have determined that the only solutions are indeed
- resize() the widget at some point in time when calling that useless function actually has some effect (not in the constructor or showEvent)
- subclass and reimplement sizeHint()
- rely on stretchLastSection only.
Both 1) and 2) require you to somehow compute the correct value yourself. I went with option 2:
QSize SizedTable
::minimumSizeHint() const {
int width = 0;
for (int a = 0; a < columnCount(); ++a)
{
width += columnWidth(a);
}
size.setWidth(width + (columnCount() * 1));
int height = horizontalHeader()->height();
for (int a = 0; a < rowCount(); ++a)
{
height += rowHeight(a);
}
size.setHeight(height + (rowCount() * 1));
return size;
}
QSize SizedTable
::sizeHint() const {
return minimumSizeHint();
}
QSize SizedTable::minimumSizeHint() const
{
QSize size(QTableWidget::sizeHint());
int width = 0;
for (int a = 0; a < columnCount(); ++a)
{
width += columnWidth(a);
}
size.setWidth(width + (columnCount() * 1));
int height = horizontalHeader()->height();
for (int a = 0; a < rowCount(); ++a)
{
height += rowHeight(a);
}
size.setHeight(height + (rowCount() * 1));
return size;
}
QSize SizedTable::sizeHint() const
{
return minimumSizeHint();
}
To copy to clipboard, switch view to plain text mode
Setting horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents) in the subclassed custom widget causes header style to be lost. Before rows (or columns) are deleted or added, QSizePolicy::Expanding must be
set, and after that is finished, QSizePolicy::Fixed must be set again. Not using size policies and just calling
table->setMinimumSize(table->sizeHint()) and table->setMaximumSize(table->sizeHint()) kills last section stretch.
I really thought that it would be easy to give these two orders to any Qt widget: "Resize yourself to the size of your own data, and then maintain that size". QTableWidget is not much of a "convenience" class in this regard.
Combined with the solution to my scrollarea problem I have finally managed to obtain a scrolling page that has several fixed-size tables whose size is not known until runtime, without extra whitespace around the tables. But, now the last section will not stretch!
I'll try to fix that and will come back with either a solution or a plea for more help...
Bookmarks