Restore window state of recreated top level widget, not closest QWindow

When we recreate a top level QWidget due to the RHI config not matching,
we need to re-apply the window state, as the state is lost on destroy().

But we should do this on the QWidget we are recreating, not on its
closest QWindow. These are usually the same, so it didn't matter in
practice for most cases (besides the more convoluted way of getting
to the QWidget's window).

But if the top level widget does not have a winId yet, the call to find
the closest QWindow via QWidgetPrivate::windowHandle() will traverse from
the top level widget to its transient parent widget, via nativeParentWidget,
which is strange and likely a bug.

As a result of that, we would store the window state of the transient
parent widget, and then, once we had created() the new top level, we
would apply the window state to the top level, which now had a winId.

We can simplify all of this by just storing and applying the window
state on the widget we are re-creating. There's no need to go via
the closest QWindow for this. We do however need to set the window
state on the widget's window, as going via QWidget will just bail
out since the window state hasn't changed.

Amends c88211d1e4ac12eb2ae4990703a4f73c7085d624

Fixes: QTBUG-119795
Change-Id: I0ad6e7bbac5f29d095cc643e9a7094784e9a2122
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
(cherry picked from commit aecedfed9ab763513af8fda5a2b208b263da3f65)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit fe434acac664b5e4a94b6816e44a4ba460214fd2)
(cherry picked from commit ab683da496d58631e58595b3e0d6862e0d377e6a)
This commit is contained in:
Tor Arne Vestbø 2024-02-06 17:28:01 +01:00 committed by Qt Cherry-pick Bot
parent 2222cae859
commit 936c8db2c6

View File

@ -10806,10 +10806,11 @@ void QWidget::setParent(QWidget *parent, Qt::WindowFlags f)
recreate = true; recreate = true;
} }
if (recreate) { if (recreate) {
auto oldState = d->windowHandle(QWidgetPrivate::WindowHandleMode::Closest)->windowState(); const auto windowStateBeforeDestroy = newtlw->windowState();
newtlw->destroy(); newtlw->destroy();
newtlw->create(); newtlw->create();
d->windowHandle(QWidgetPrivate::WindowHandleMode::Closest)->setWindowState(oldState); Q_ASSERT(newtlw->windowHandle());
newtlw->windowHandle()->setWindowStates(windowStateBeforeDestroy);
} }
} }
} }