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 <adamm@zombino.com>
Reviewed-by: Shawn Rutledge <shawn.rutledge@theqtcompany.com>
This commit is contained in:
Błażej Szczygieł 2016-01-05 15:25:12 +01:00
parent eb0b03c579
commit 7e28079484

View File

@ -964,18 +964,38 @@ qreal QGuiApplication::devicePixelRatio() const
*/
QWindow *QGuiApplication::topLevelAt(const QPoint &pos)
{
QList<QScreen *> screens = QGuiApplication::screens();
QList<QScreen *>::const_iterator screen = screens.constBegin();
QList<QScreen *>::const_iterator end = screens.constEnd();
const QList<QScreen *> screens = QGuiApplication::screens();
if (!screens.isEmpty()) {
const QList<QScreen *> primaryScreens = screens.first()->virtualSiblings();
QScreen *windowScreen = Q_NULLPTR;
while (screen != end) {
if ((*screen)->geometry().contains(pos)) {
const QPoint devicePosition = QHighDpi::toNativePixels(pos, *screen);
return (*screen)->handle()->topLevelAt(devicePosition);
// Find the window on the primary virtual desktop first
foreach (QScreen *screen, primaryScreens) {
if (screen->geometry().contains(pos)) {
windowScreen = screen;
break;
}
++screen;
}
return 0;
// 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);
}
}
return Q_NULLPTR;
}
/*!