From fe434acac664b5e4a94b6816e44a4ba460214fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 6 Feb 2024 17:28:01 +0100 Subject: [PATCH] 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 Pick-to: 6.6 6.5 Change-Id: I0ad6e7bbac5f29d095cc643e9a7094784e9a2122 Reviewed-by: Volker Hilsheimer Reviewed-by: Richard Moe Gustavsen (cherry picked from commit aecedfed9ab763513af8fda5a2b208b263da3f65) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/kernel/qwidget.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp index baababe09ae..2734d598517 100644 --- a/src/widgets/kernel/qwidget.cpp +++ b/src/widgets/kernel/qwidget.cpp @@ -10898,10 +10898,11 @@ void QWidget::setParent(QWidget *parent, Qt::WindowFlags f) recreate = true; } if (recreate) { - auto oldState = d->windowHandle(QWidgetPrivate::WindowHandleMode::Closest)->windowState(); + const auto windowStateBeforeDestroy = newtlw->windowState(); newtlw->destroy(); newtlw->create(); - d->windowHandle(QWidgetPrivate::WindowHandleMode::Closest)->setWindowState(oldState); + Q_ASSERT(newtlw->windowHandle()); + newtlw->windowHandle()->setWindowStates(windowStateBeforeDestroy); } } }