[Interest] geometry() for a widget that hasn't been shown

André Somers andre at familiesomers.nl
Wed Sep 23 10:00:48 CEST 2015


Op 21-9-2015 om 19:33 schreef John Weeks:
> If this is a repeat, my apologies. I can't tell if I saw this arrive on the list!
>
>
>
> In general a QWidget that hasn't been made visible yet gives bogus results for QWidget::geometry(). Qt only guarantees that a call to setGeometry() will result in a Resize event when the window is made visible. But we often need to ask a window or child widget how big it is before it is made visible in order to do various kinds of calculations. These calculations are sometimes used for things that preclude waiting for the window to become visible.
>
> Is there a way to force the Resize events (and all the layout calculation machinery that goes with it) before it is visible? Using Qt 5.5, I see that if you call QWidget::grab(), it will call a static function sendResizeEvents() that does exactly what I want:
>
> QPixmap QWidget::grab(const QRect &rectangle)
> {
>     Q_D(QWidget);
>     if (testAttribute(Qt::WA_PendingResizeEvent) || !testAttribute(Qt::WA_WState_Created))
>         sendResizeEvents(this);
>
> Is there some other way to get this to happen? I suppose I could just call grab() and throw away the QPixmap, but that seems like an awful kludge...
>
In the past, I have resorted to first showing the window off-screen, 
getting its measurements, do my thing, and only then move it to the 
visible area of the screen. It works, but it is a drag. In my case it 
was needed in order to make a dialog that nicely resizes using an 
animation when different options are selected.

André




More information about the Interest mailing list