From f2347077f503d103974636cae9319d127714e2e4 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Fri, 24 Apr 2020 11:41:05 +0200 Subject: [PATCH] Always open a text control's context menu on the same screen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's very disorienting when a context menu pops up on a different screen from where the right mouse button was clicked. QWidgetPrivate::setScreenForPoint() can give incorrect results in multi-screen desktop configurations; but we can give the menu a hint by setting initialScreenIndex, which was originally used only when QDesktopScreen or QDesktopScreenWidget is the parent (see b3fc5e1ea3eb4fe838ac716aaca4efaa5de5a814 ). Task-number: QTBUG-76162 Pick-to: 5.15 Pick-to: 5.12 Change-Id: Icd4abdc32564430147e6c63a8ea70d483d25cddd Reviewed-by: Tor Arne Vestbø Reviewed-by: Volker Hilsheimer --- src/widgets/widgets/qwidgettextcontrol.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/widgets/widgets/qwidgettextcontrol.cpp b/src/widgets/widgets/qwidgettextcontrol.cpp index e32f779bb0..b87b4e54fa 100644 --- a/src/widgets/widgets/qwidgettextcontrol.cpp +++ b/src/widgets/widgets/qwidgettextcontrol.cpp @@ -58,6 +58,7 @@ #include "private/qapplication_p.h" #include "private/qtextdocumentlayout_p.h" #include "private/qabstracttextdocumentlayout_p.h" +#include "private/qmenu_p.h" #include "qtextdocument.h" #include "private/qtextdocument_p.h" #include "qtextlist.h" @@ -1943,6 +1944,10 @@ void QWidgetTextControlPrivate::contextMenuEvent(const QPoint &screenPos, const if (!menu) return; menu->setAttribute(Qt::WA_DeleteOnClose); + if (auto *window = static_cast(parent)->window()->windowHandle()) { + QMenuPrivate::get(menu)->topData()->initialScreenIndex = + QGuiApplication::screens().indexOf(window->screen()); + } menu->popup(screenPos); #endif }