Always open a text control's context menu on the same screen

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
b3fc5e1ea3 ).

Task-number: QTBUG-76162
Pick-to: 5.15
Pick-to: 5.12
Change-Id: Icd4abdc32564430147e6c63a8ea70d483d25cddd
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Shawn Rutledge 2020-04-24 11:41:05 +02:00
parent e9b8837c31
commit f2347077f5
1 changed files with 5 additions and 0 deletions

View File

@ -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<QWidget *>(parent)->window()->windowHandle()) {
QMenuPrivate::get(menu)->topData()->initialScreenIndex =
QGuiApplication::screens().indexOf(window->screen());
}
menu->popup(screenPos);
#endif
}