From b50f0244c8f1b8e229a71018f39ac373d0ba6a1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Szczygie=C5=82?= Date: Fri, 1 Jan 2016 13:56:04 +0100 Subject: QDesktopWidget::screenNumber(QPoint): fix handling of virtual desktops On X11, QXcbVirtualDesktop represents an X11 screen while QScreen represents an X11 output. In the case that there are multiple screens (possibly with multiple outputs), calculate the screen number correctly: Find the screen index on the primary virtual desktop first to avoid obtaining a screen index which doesn't belong to the primary virtual desktop when screen geometry is similar. Change-Id: I4cbb29b7aa7cd2125759ffbbbe3db4e934feaeae Reviewed-by: Shawn Rutledge --- src/widgets/kernel/qdesktopwidget.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/widgets/kernel/qdesktopwidget.cpp b/src/widgets/kernel/qdesktopwidget.cpp index e586be206e..a1c2aebbe6 100644 --- a/src/widgets/kernel/qdesktopwidget.cpp +++ b/src/widgets/kernel/qdesktopwidget.cpp @@ -239,12 +239,25 @@ int QDesktopWidget::screenNumber(const QWidget *w) const int QDesktopWidget::screenNumber(const QPoint &p) const { - QList screens = QGuiApplication::screens(); - - for (int i = 0; i < screens.size(); ++i) - if (screens.at(i)->geometry().contains(p)) - return i; - + const QList screens = QGuiApplication::screens(); + if (!screens.isEmpty()) { + const QList primaryScreens = screens.first()->virtualSiblings(); + // Find the screen index on the primary virtual desktop first + foreach (QScreen *screen, primaryScreens) { + if (screen->geometry().contains(p)) + return screens.indexOf(screen); + } + // If the screen index is not found on primary virtual desktop, find + // the screen index 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 (screens.count() != primaryScreens.count()) { + for (int i = 1; i < screens.size(); ++i) { + if (screens[i]->geometry().contains(p)) + return i; + } + } + } return primaryScreen(); //even better would be closest screen } -- cgit v1.2.3