View Full Version : Force geometry update of hidden widget

8th December 2009, 21:51
Is there any way to force the update of geometry for a hidden (i.e. isVisible() == false) widget? It seems like all of the geometry updating routines wait until the widget is shown to actually update the layout/geometry.

8th December 2009, 22:59
There is a way but it is a hack and you shouldn't do it, especially that it's completely meaningless. Why do you need it?

8th December 2009, 23:32
I have a QMenu with an embedded widget (provided by a QWidgetAction). The content of the embedded widget is updated between showings of the menu which could cause the size of the embedded widget to change.

It appears to me that when the menu tries to calculate its new size for the next show the widget's layout has not been updated to reflect the new required size because the updates are applied while the widget was not visible. The widget's sizeHint is not updated until it is actually shown, too late to influence the size of the menu. Consequently, the menu uses the stale size of the widget, and the size of the menu is always lagging behind the size of the widget by one update cycle.

I was thinking if I could force the widget to update its layout (really its sizeHint) prior to being shown, the QMenu could obtain the correct sizeHint from the widget.

9th December 2009, 00:29
Try calling ensurePolished() on the menu and/or the widget before you try to display the menu. Maybe this will be enough.

9th December 2009, 01:26
Alas, that does not seem to do the trick.

I have hit upon a work-around that I can make use of for the time being.

If the top-level layout of the widget is altered then the widget will report the correct sizeHint to the menu prior to being shown. However, if the contents of only child widget layouts are altered, changes do not percolate up until the widget is actually being shown (too late).

Forcing either only a single top-level layout or a change to the top-level layout prior to being shown seems to do the trick so that the correct sizeHint is reported to the QMenu.


This is really still pretty dirty and I would still be interested if anyone has a better way.

9th December 2009, 01:28
You can call invalidate() on the layout. It should recalculate itself then. If not then there is this secret attribute that convinces the widget it is being shown and then you should be able to force its layout to recalculate. But try other things first, this is not something very reliable.

9th December 2009, 01:39
Yeah, I had tried 'invalidate' as like the first thing. No dice.
I was kind of surprised it had no effect where as actually causing a change to the layout does have an effect.

secret attribute that convinces the widget it is being shown
Which attribute is that?

9th December 2009, 09:12
Qt::WA_WState_ExplicitShowHide set to true and Qt::WA_WState_Hidden set to false. Be sure to revert the values (especially the second one) before you actually show the widget on screen.