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.
c ++ qt qt5 multiscreen
armitage
source share