tags:

views:

207

answers:

3

What I am trying to do is render a qwidget onto a different window (manually using a QPainter)

I have a QWidget (w) with a layout and a bunch of child controls. w is hidden. Until w is shown, there is no layout calculations happening, which is expected.

When I call w->render(painter, w->mapToGlobal(QPoint(0,0)), I get a bunch of controls all overlapping each other.
w->layout()->activate();w->layout()->update() doesn't seem to do anything.

Is there a way to force the layout to happen without showing w?

+1  A: 

Try with the QWidget::sizeHint() method, which is supposed to return the size of the widget once laid out.

gregseth
I tried that, and it did give me back reasonable sizes.. but it didn't update the positions! So the sizes look right, but they are still on top of each other.
Chris
A: 

QWidget::adjustSize works for me. But I did some tricky things: because widget is invisible, I have to explicitly set to visible. Sometimes calling adjustSize of the top most widget doesn't work, but call children first then call top-most widget works. Good Luck!

Mason Chang
When you have to explicitly set the widget to visible how does this answer the question...?
Ton van den Heuvel
In my case, the widget should be visible, but because I added widgets to parent when parent is invisible, I ran into this case. In addition, set children to visible does not impact the result in my case.
Mason Chang
+3  A: 

Forcing a layout calculation on a widget without showing it on the screen:

widget->setAttribute(Qt::WA_DontShowOnScreen);
widget->show();

The show() call will force the layout calculation, and Qt::WA_DontShowOnScreen ensures that the widget is not explicitly shown.

Very useful for command line unit tests, where you do not want the user's desktop to be cluttered with random widgets popping up left and right.

Ton van den Heuvel