Avoid segfault in QWindow::mapFromGlobal when platformWindow is null

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ø <tor.arne.vestbo@qt.io>
This commit is contained in:
Mitch Curtis 2023-11-29 10:10:07 +08:00
parent f96a17225f
commit f60fb8f417

View File

@ -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;