chacham15
chacham15

Reputation: 14251

How do I get the height of a layout after inserting new items?

In Qt:

QVBoxLayout *layout = (QVBoxLayout*)this->layout();

printf("Before: %d %d\n", this->height(), layout->totalSizeHint().height());

QWidget *widget = new SubWidget();
layout->insertWidget(0, widget);
this->updateGeometry();
this->adjustSize();
this->update();
this->repaint();

printf("After: %d %d %d\n", this->height(), layout->totalSizeHint().height(), widget->height());

The problem is that I get the same numbers for before and after despite the widgets height being nonzero (about 400 in my case). Why?

Upvotes: 1

Views: 105

Answers (1)

alexisdm
alexisdm

Reputation: 29886

The window is already visible, and the widgets you are inserting aren't yet visible. The layout makes them visible asynchronously (with an invokeMethod call in Qt::QueuedConnection mode).

You can either wait for the call to actually take place with QApplication::processEvents() or show them yourself:

 QWidget *widget = new SubWidget();
 layout->insertWidget(0, widget);
 qApp->processEvents();
 // or
 widget->show();

Upvotes: 2

Related Questions