Use old position to set window position after recreation

Like window state and visibility, the window position should also
remain the same after the window is recreated.

Fixes: QTBUG-128790
Change-Id: Iad8599da7ef83eff6a93f7f071e6b452491a0d87
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Morteza Jamshidi 2024-09-23 12:57:45 +02:00
parent 9261b68395
commit d4ccceabf4
2 changed files with 27 additions and 0 deletions

View File

@ -10908,10 +10908,12 @@ void QWidget::setParent(QWidget *parent, Qt::WindowFlags f)
<< "to support" << surfaceType;
const auto windowStateBeforeDestroy = newParentWithWindow->windowState();
const auto visibilityBeforeDestroy = newParentWithWindow->isVisible();
const auto positionBeforeDestroy = newParentWithWindow->pos();
newParentWithWindow->destroy();
newParentWithWindow->create();
Q_ASSERT(newParentWithWindow->windowHandle());
newParentWithWindow->windowHandle()->setWindowStates(windowStateBeforeDestroy);
newParentWithWindow->move(positionBeforeDestroy);
QWidgetPrivate::get(newParentWithWindow)->setVisible(visibilityBeforeDestroy);
} else if (auto *backingStore = newParentWithWindow->backingStore()) {
// If we don't recreate we still need to make sure the native parent

View File

@ -7,6 +7,7 @@
#include <QScrollArea>
#include <QScrollBar>
#include <QApplication>
#include <QMainWindow>
#include <private/qhighdpiscaling_p.h>
#include <private/qwidget_p.h>
@ -261,6 +262,7 @@ private slots:
void scroll();
void paintOnScreenUpdates();
void evaluateRhi();
void rhiRecreateMaintainsWindowProperties();
#if defined(QT_BUILD_INTERNAL)
void scrollWithOverlap();
@ -798,6 +800,29 @@ void tst_QWidgetRepaintManager::evaluateRhi()
#endif // QT_CONFIG(opengl)
}
void tst_QWidgetRepaintManager::rhiRecreateMaintainsWindowProperties()
{
const auto *integration = QGuiApplicationPrivate::platformIntegration();
if (!integration->hasCapability(QPlatformIntegration::RhiBasedRendering))
QSKIP("Platform does not support RHI based rendering");
#if !QT_CONFIG(opengl)
QSKIP("Platform does not support OpenGL RHI based rendering");
#endif
// Reparenting Rhi widget into a window causes the window to be
// recreated, but after recreation the window properties such as
// window position must remain the same
QMainWindow topLevel;
topLevel.show();
QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
QPoint windowPos = topLevel.pos();
RhiWidget nativeRhiWidget(QPlatformBackingStoreRhiConfig::OpenGL, &topLevel);
QVERIFY(QWidgetPrivate::get(&topLevel)->usesRhiFlush);
QVERIFY(QWidgetPrivate::get(&topLevel)->rhi());
QCOMPARE(topLevel.pos(), windowPos);
}
#if defined(QT_BUILD_INTERNAL)
/*!