From f60fb8f41741c2ca4dbd02b70d56e561be64d673 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Wed, 29 Nov 2023 10:10:07 +0800 Subject: [PATCH] Avoid segfault in QWindow::mapFromGlobal when platformWindow is null MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the window hasn't been shown yet (as is the case when running ./tst_qquickmaterialstyle Material::test_background:drawer in qtdeclarative), platformWindow can be null. Check for that and use the pre-67fa2585ac48e64972d1c0a20b3add5c3ef72e51 code path instead. Fixes: QTBUG-119517 Change-Id: I8333578b94f91b5a2994875da6dc568a360d2edf Reviewed-by: Tor Arne Vestbø --- src/gui/kernel/qwindow.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index 13bcc56f950..178b1f51e54 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -2888,7 +2888,12 @@ QPointF QWindow::mapFromGlobal(const QPointF &pos) const // Calculate local position in the native coordinate system. (See comment for the // corresponding mapToGlobal() code above). QPointF nativeGlobalPos = QHighDpi::toNativeGlobalPosition(pos, this); - QPointF nativeWindowGlobalPos = d->platformWindow->mapToGlobal(QPoint(0,0)).toPointF(); + // Get the native window position directly from the platform window + // if available (it can be null if the window hasn't been shown yet), + // or fall back to scaling the QWindow position. + QPointF nativeWindowGlobalPos = d->platformWindow + ? d->platformWindow->mapToGlobal(QPoint(0,0)).toPointF() + : QHighDpi::toNativeGlobalPosition(QPointF(d->globalPosition()), this); QPointF nativeLocalPos = nativeGlobalPos - nativeWindowGlobalPos; QPointF deviceIndependentLocalPos = QHighDpi::fromNativeLocalPosition(nativeLocalPos, this); return deviceIndependentLocalPos;