[Interest] Update widget geometry BEFORE window shown

Axel Spoerl axel.spoerl at qt.io
Wed May 29 13:03:53 CEST 2024


Hi David,

a few colleagues and I are currently exploring options to optimize our UI tests in CI.
Your requirement is interesting in that context: It would sometimes be handy to know in advance, how stuff is going to look before it's rendered onto a screen.

The calculation of a widget's geometry is tied to the process of showing. There are good reasons for that, e.g. native window decoration or native elements like checkboxes, buttons, menus. Platform specific approaches to window minimisation, maximisation, full-screen also play a role.

Our documentation (https://doc.qt.io/qt-6/qwidget.html#geometry-prop andhttps://doc.qt.io/qt-6/application-windows.html#window-geometry) doesn't promise accurate geometry, before a widget is shown. It could be clearer on that particular point. Such a requirement hasn't been an issue, at least AFAIK.

That said: When we test geometries, or even rendering, we always compare against a base line. In other words: We take screenshots or read geometries from visible widgets and compare against those.

Maybe there are ways to trick the system into a geometry calculation of invisible widgets. I don't know of any, and if they exist, they are probably not reliable.
I don't see a solid implementation, meeting your requirement. Not now, not in the forseeable future.

Cheers
Axel
________________________________
Von: Interest <interest-bounces at qt-project.org> im Auftrag von David M. Cotter <dave at kjams.com>
Gesendet: Mittwoch, 29. Mai 2024 01:32
An: interest at qt-project.org <interest at qt-project.org>
Betreff: Re: [Interest] Update widget geometry BEFORE window shown

Tried that too, to no avail

static void activateLayoutsRecursive(QObject *objectP)
{
if (objectP) {

for (auto* childP: objectP->children()) {
activateLayoutsRecursive(childP);
}

QWidget* widgetP(dynamic_cast<QWidget*>(objectP));

if (widgetP) {
widgetP->updateGeometry();

QLayout* layoutP(widgetP->layout());

if (layoutP) {
layoutP->invalidate();
layoutP->update();
layoutP->activate();
}
}
}
}


On May 27, 2024, at 1:07 AM, Tony Rietwyk <tony at rightsoft.com.au> wrote:


Hi Dave,

I have had a similar issue at various times, especially in Qt4, but not as much in Qt5 & 6.  A few places still had problems when adjusting after creating or deleting widgets, or switching pages in an embedded page control.  This is the routine I use:

void activateLayoutsRecursive(QWidget *w)
{
    if (w)
    {
        // Depth first traversal.
        for (auto k : w->children())
        {
            if (auto z = qobject_cast<QWidget *>(k))
                activateLayoutsRecursive( z );
        }

        if (w->layout())
        {
            w->layout()->invalidate();
            w->layout()->activate();
            //qCDebug(lc) << w->objectName() << w->layout()->totalMinimumSize();
        }
    }
}

I hope that helps!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20240529/d0a5844a/attachment.htm>


More information about the Interest mailing list