c++ - Qt 5, get the mouse position in a screen -
first of all, i'd mention found related post how mouse position on screen in qt? "just didn't work" me. made tests, , results didn't work expected, decided make new post talk test made , find alternative solution.
that's code used make 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()); // position without screen. qpoint pos = qcursor::pos(); printf("pos: %d, %d \n", pos.x(), pos.y());
what expecting, 1 screen return valid position, since cursor @ 1 screen, not on both. it's not case, both positions (pos0
, pos1
) has same value, can see on output:
screen0 dvi-d-0 screen1 hdmi-0 pos 0: 1904, 1178 pos 1: 1904, 1178 pos: 1904, 1178
since both positions has same values, can't know @ screen cursor. don't know if that's normal behavior or bug, since documentation doesn't happens when screen argument isn't screen mouse is.
my idea, open/launch application (executed qt daemon must detect selected screen) screen mouse is. know libx11 it's possible, because did in past, need work qt 5, , can't figure out how detect selected screen qt.
i made other tests, using qapplication
, qdesktopwidget
classes no luck.
that's weird. workaround, try this:
qpoint globalcursorpos = qcursor::pos(); int mousescreen = qapp->desktop()->screennumber(globalcursorpos);
now know screen cursor in. find cursor position within screen doing this:
qrect mousescreengeometry = qapp->desktop()->screen(mousescreen)->geometry(); qpoint localcursorpos = globalcursorpos - mousescreengeometry.topleft();
Comments
Post a Comment