Qt 5, get the mouse position on the screen

First of all, I would like to mention that I found that the linked post How to get the mouse position on the screen in Qt? but it โ€œjust didnโ€™t work for me. I did some tests and the results didnโ€™t work as I expected, so I decided to make a new record to talk about the testing that I did and find an alternative solution.

This is the code I used to test:

QScreen *screen0 = QApplication::screens().at(0); QScreen *screen1 = QApplication::screens().at(1); printf("screen0 %s \n", screen0->name().toStdString().c_str()); printf("screen1 %s \n", screen1->name().toStdString().c_str()); // Position on first screen. QPoint pos0 = QCursor::pos(screen0); // Position on second screen. QPoint pos1 = QCursor::pos(screen1); printf("pos 0: %d, %d \n", pos0.x(), pos0.y()); printf("pos 1: %d, %d \n", pos1.x(), pos1.y()); // Get position without screen. QPoint pos = QCursor::pos(); printf("pos: %d, %d \n", pos.x(), pos.y()); 

I expected that only one screen will return the correct position, since the cursor is on only one screen, and not on both. But this is not so, both positions ( pos0 and pos1 ) have exactly the same value as the output:

 screen0 DVI-D-0 screen1 HDMI-0 pos 0: 1904, 1178 pos 1: 1904, 1178 pos: 1904, 1178 

Since both positions have the same values, I cannot know which screen the cursor is on. I do not know the normal behavior or error, since the documentation does not say what happens when the screen argument is not the screen on which the mouse is located.

My idea is to open / launch an application (executed by the Qt daemon that should detect the selected screen) on the screen where the mouse is located. I know that with libX11 this is possible because I have done this in the past, but I need to work with Qt 5 and I cannot figure out how to detect the selected screen with Qt.

I also conducted other tests using QApplication and QDesktopWidget classes with no luck.

+8
c ++ qt qt5 multiscreen
source share
4 answers

This is really weird. As a workaround, you can try the following:

 QPoint globalCursorPos = QCursor::pos(); int mouseScreen = qApp->desktop()->screenNumber(globalCursorPos); 

Now you know which screen the cursor is on. Then you can find the cursor position on this screen:

 QRect mouseScreenGeometry = qApp->desktop()->screen(mouseScreen)->geometry(); QPoint localCursorPos = globalCursorPos - mouseScreenGeometry.topLeft(); 
+3
source share

To find out which screen you are on, you can iterate QGuiApplication::screens() and check if the cursor fits in the geometry of the screen.

Here is a more complex example of calculating your own cursor position (pay attention to the additional work required to work with high-resolution DPI screens):

 QPoint getNativeCursorPosition() { QPoint pos = cursorPosToNative(QCursor::pos()); // Cursor positions from Qt are calculated in a strange way, the offset to // the origin of the current screen is in device-independent pixels while // the origin itself is native! for (QScreen *screen : QGuiApplication::screens()) { QRect screenRect = screen->geometry(); if (screenRect.contains(pos)) { QPoint origin = screenRect.topLeft(); return origin + (pos - origin) * screen->devicePixelRatio(); } } // should not happen, but try to find a good fallback. return pos * qApp->devicePixelRatio(); } 
+2
source share

It seems that this cannot be done with Qt (at least with my system configuration, and it seems to be on Windows as well) I decided to use libX11 for an implementation that works like a charm.

This is not an ideal solution because I wanted to use only Qt, but it works.

0
source share

This may seem like a trivial solution, but it works on my KDE (I ran into the same problems initially). If you want to determine the local coordinates of the mouse relative to the widget (this will be in the pixels of the device and relative to the upper left corner of the widget that I believe in), you can use

 QWidget::mapFromGlobal(QCursor::pos()); 

i.e. calling this->mapFromGlobal .

0
source share

All Articles