From 7e28079484ec12ce0609419262b869354e7678a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Szczygie=C5=82?= Date: Tue, 5 Jan 2016 15:25:12 +0100 Subject: QtGui: Fix obtaining the top level window at a point Find the top level window on the primary virtual desktop first to avoid obtaining a window which doesn't belong to the primary virtual desktop when screen geometry is similar. Change-Id: I78fdfa0b5146d0ba9b912338adeb612c102c4ac3 Reviewed-by: Adam Majer Reviewed-by: Shawn Rutledge --- src/gui/kernel/qguiapplication.cpp | 40 ++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'src/gui/kernel') diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 00245b25b0..00e3e2fcf4 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -964,18 +964,38 @@ qreal QGuiApplication::devicePixelRatio() const */ QWindow *QGuiApplication::topLevelAt(const QPoint &pos) { - QList screens = QGuiApplication::screens(); - QList::const_iterator screen = screens.constBegin(); - QList::const_iterator end = screens.constEnd(); - - while (screen != end) { - if ((*screen)->geometry().contains(pos)) { - const QPoint devicePosition = QHighDpi::toNativePixels(pos, *screen); - return (*screen)->handle()->topLevelAt(devicePosition); + const QList screens = QGuiApplication::screens(); + if (!screens.isEmpty()) { + const QList primaryScreens = screens.first()->virtualSiblings(); + QScreen *windowScreen = Q_NULLPTR; + + // Find the window on the primary virtual desktop first + foreach (QScreen *screen, primaryScreens) { + if (screen->geometry().contains(pos)) { + windowScreen = screen; + break; + } + } + + // If the window is not found on primary virtual desktop, find it on all screens + // except the first which was for sure in the previous loop. Some other screens + // may repeat. Find only when there is more than one virtual desktop. + if (!windowScreen && screens.count() != primaryScreens.count()) { + for (int i = 1; i < screens.size(); ++i) { + QScreen *screen = screens[i]; + if (screen->geometry().contains(pos)) { + windowScreen = screen; + break; + } + } + } + + if (windowScreen) { + const QPoint devicePosition = QHighDpi::toNativePixels(pos, windowScreen); + return windowScreen->handle()->topLevelAt(devicePosition); } - ++screen; } - return 0; + return Q_NULLPTR; } /*! -- cgit v1.2.3