From 54f328f0e8205480749a6d8d2ebe0e58cb1cdb67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lund=20Martsum?= Date: Tue, 14 Jun 2022 08:52:47 +0200 Subject: [PATCH] Fix QDockWidget move between screens with different dpr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When partly moved back and forth between screens with different dprs (device pixel ratios) unexpected jumps and size changes could occur. (See the linked issue for details) This patch maps global coordinates to native ones and vice versa (in QDockWidgetPrivate::mouseMoveEvent()), so that the calculated position is the right coordinate on the right screen. Pick-to: 6.4 6.3 6.2 5.15 Fixes: QTBUG-104205 Change-Id: I0e59792a946e0444fed2e2b857f2f8b140afc9b7 Reviewed-by: Axel Spoerl Reviewed-by: Tor Arne Vestbø Reviewed-by: Shawn Rutledge --- src/widgets/widgets/qdockwidget.cpp | 29 ++++++++++++++++++++++++++++- src/widgets/widgets/qdockwidget_p.h | 2 ++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/widgets/widgets/qdockwidget.cpp b/src/widgets/widgets/qdockwidget.cpp index 7da1023f04..14022aa830 100644 --- a/src/widgets/widgets/qdockwidget.cpp +++ b/src/widgets/widgets/qdockwidget.cpp @@ -20,6 +20,7 @@ #include #include +#include #include "qdockwidget_p.h" #include "qmainwindowlayout_p.h" @@ -752,6 +753,8 @@ void QDockWidgetPrivate::initDrag(const QPoint &pos, bool nca) state = new QDockWidgetPrivate::DragState; state->pressPos = pos; + state->globalPressPos = q->mapToGlobal(pos); + state->widgetInitialPos = q->pos(); state->dragging = false; state->widgetItem = nullptr; state->ownWidgetItem = false; @@ -981,7 +984,31 @@ bool QDockWidgetPrivate::mouseMoveEvent(QMouseEvent *event) if (state && state->dragging && !state->nca) { QMargins windowMargins = q->window()->windowHandle()->frameMargins(); QPoint windowMarginOffset = QPoint(windowMargins.left(), windowMargins.top()); - QPoint pos = event->globalPosition().toPoint() - state->pressPos - windowMarginOffset; + + // TODO maybe use QScreen API (if/when available) to simplify the below code. + const QScreen *orgWdgScreen = QGuiApplication::screenAt(state->widgetInitialPos); + const QScreen *screenFrom = QGuiApplication::screenAt(state->globalPressPos); + const QScreen *screenTo = QGuiApplication::screenAt(event->globalPosition().toPoint()); + const QScreen *wdgScreen = q->screen(); + + QPoint pos; + if (Q_LIKELY(screenFrom && screenTo && wdgScreen && orgWdgScreen)) { + const QPoint nativeWdgOrgPos = QHighDpiScaling::mapPositionToNative( + state->widgetInitialPos, orgWdgScreen->handle()); + const QPoint nativeTo = QHighDpiScaling::mapPositionToNative( + event->globalPosition().toPoint(), screenTo->handle()); + const QPoint nativeFrom = QHighDpiScaling::mapPositionToNative(state->globalPressPos, + screenFrom->handle()); + + // Calculate new nativePos based on startPos + mouse delta move. + const QPoint nativeNewPos = nativeWdgOrgPos + (nativeTo - nativeFrom); + + pos = QHighDpiScaling::mapPositionFromNative(nativeNewPos, wdgScreen->handle()) + - windowMarginOffset; + } else { + // Fallback in the unlikely case that source and target screens could not be established + pos = event->globalPosition().toPoint() - state->pressPos - windowMarginOffset; + } QDockWidgetGroupWindow *floatingTab = qobject_cast(parent); if (floatingTab && !q->isFloating()) diff --git a/src/widgets/widgets/qdockwidget_p.h b/src/widgets/widgets/qdockwidget_p.h index 8e9f80d412..326c55cc78 100644 --- a/src/widgets/widgets/qdockwidget_p.h +++ b/src/widgets/widgets/qdockwidget_p.h @@ -41,6 +41,8 @@ class QDockWidgetPrivate : public QWidgetPrivate struct DragState { QPoint pressPos; + QPoint globalPressPos; + QPoint widgetInitialPos; bool dragging; QLayoutItem *widgetItem; bool ownWidgetItem;