[Qt-interest] size of a widget with layout before shown

Karol Krizka kkrizka at gmail.com
Sun May 31 14:40:19 CEST 2009


On Sun, May 31, 2009 at 2:33 PM, Frank Mertens <frank at cyblogic.de> wrote:
> Karol Krizka wrote:
>> On Sat, May 30, 2009 at 8:25 PM, Frank Mertens <frank at cyblogic.de> wrote:
>>> Frequently I face the same problem with Qt's layout system.
>>> How to determine the size a widget would take if shown as top-level widget?
>>> Let's say a QMenu? -- Need to have it placed relative to its size.
>>>
>>> I'm used to hack around this issue by show()/hide() and some forced
>>> manual event processing.
>>>
>>> But is there no clean solution?
>> I'm actually attempting to solve the same problem, so let me know if
>> you come up with anything clean.
>>
>> For now, I'm also using a show()/hide() trick. Basically, I start the
>> widget as hidden. When I want the size, I do the following:
>> setUpdatesEnabled(false);
>> widget->show();
>> widget->hide();
>> setUpdatesEnabled(true);
>>
>> When the widget is hidden, I use a spacer to reserve space for it.
>>
>
> After fighting down the same path again yesterday I figured things improved
> with Qt 4.5. I have a custom widget, which I use as popup menu and I'm
> using QMenu for context menus. QMenu::sizeHint() now always delivers meaningful
> sizes, before shown the first time. For my custom widget I had to do the
> following for Qt 4.4:
>
>        layout()->activate();
>        QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
>        QSize sz = sizeHint();
>
> On Qt 4.5 I can safely comment out the second line. If commenting out the
> first two lines I get only a meaningful sizeHint() if the widgets layout
> has not been changed.
>
Ah, interesting solution. But that would not work for me, because I'm
also interested in the position of the widget.

--
Cheers,
Karol Krizka
http://www.krizka.net




More information about the Qt-interest-old mailing list