Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt 5, get the mouse position in a screen

First of all, I'd like to mention that I found that related post How to get the mouse position on the screen in Qt? but it "just didn't work" for me. I made some tests, and the results didn't work as I expected, so I decided to make a new post to talk about the test I made and to find an alternative solution.

That's the code I used to make the 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());

What I was expecting, is that only one screen would return a valid position, since the cursor is only at one screen, not on both. But it's not the case, the both positions (pos0 and pos1) has the exactly same value, as we can see on the output:

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

Since the both positions has the same values, I can't know at which screen is the cursor. I don't know if that's a normal behavior or a bug, since the documentation doesn't say what happens when the screen argument isn't the screen where the mouse is.

My idea, is to open/launch an application (executed by a Qt daemon that must detect the selected screen) to the screen where the mouse is. I know that with libX11 it's possible, because I did it in the past, but I need to work with Qt 5, and I can't figure out how to do detect the selected screen with Qt.

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

like image 975
armitage Avatar asked Aug 16 '15 21:08

armitage


People also ask

How do you get the mouse position in Qt?

To set or get the position of the mouse cursor use the static methods QCursor::pos() and QCursor::setPos().

What represents the mouse position on the screen?

The MousePosition property returns a Point that represents the mouse cursor position at the time the property was referenced. The coordinates indicate the position on the screen, not relative to the control, and are returned regardless of whether the cursor is positioned over the control.

How do you find the mouse position on a Mac?

It does not use an AppleScript, Shell Script or anything else, but if you are just looking to get the coordinates of the mouse on the screen, +command, shift 4+ will give you the coordinates. Then press escape when you have them.


2 Answers

That's really weird. As a workaround, you could try this:

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

Now you know which screen the cursor is in. Then you could find the cursor position within that screen doing this:

QRect mouseScreenGeometry = qApp->desktop()->screen(mouseScreen)->geometry();
QPoint localCursorPos = globalCursorPos - mouseScreenGeometry.topLeft();
like image 78
Rafael Monteiro Avatar answered Nov 16 '22 04:11

Rafael Monteiro


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

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

i.e. call this->mapFromGlobal.

like image 37
GPMueller Avatar answered Nov 16 '22 04:11

GPMueller