Not sure if there is a build-in solution, but this is my manual one:
Dialog::Dialog(QWidget *parent) :
QDialog(parent)
{
setupUi(this);
connect(toolButton, SIGNAL(toggled(bool)), SLOT(onToolButton(bool)));
onToolButton(false);
}
void Dialog::onToolButton(bool checked)
{
lineEdit->setVisible(checked);
int maxHeight = verticalLayout->contentsMargins().top()
+ verticalLayout->contentsMargins().bottom();
int itemsCount = 0;
for (int i = 0; i < verticalLayout->count(); ++i) {
QLayoutItem *item = verticalLayout->itemAt(i);
if (item->widget()) {
QWidget *w = item->widget();
if (w->isVisible()) {
maxHeight += w->geometry().height();
++itemsCount;
}
} else if (item->layout()) {
QLayout *l = item->layout();
maxHeight += l->geometry().height();
++itemsCount;
}
}
if (itemsCount > 1)
maxHeight += ((itemsCount - 1) * verticalLayout->spacing());
setMaximumHeight(maxHeight);
}