PDA

View Full Version : QCursor::setPos has no effect



Androiders
25th March 2019, 08:48
Hello!

I am trying to implement a "warp mouse" effect (the mouse cursor stays in the same spot on my widget even when mouse is moving) by using QCursor::setPos but it has no effect.



void QmlMouseController::warpMouse(const QPoint & p)
{
std::cout << "pre pos: " << QCursor::pos(0).x() << " " << QCursor::pos(0).y() << std::endl;
std::cout << "warp: " << p.x() << " " << p.y() << std::endl;
QCursor::setPos(QApplication::primaryScreen() ,p);
std::cout << "post pos: " << QCursor::pos(0).x() << " " << QCursor::pos(0).y() << std::endl;
}


When the above code is called with a QPoint that holds the mouse coordinates that i want to move the cursor to it prints:



pre pos: -2147483648 -2147483648
warp: 799 361
post pos: -2147483648 -2147483648


QCursor:: pos returns -2147483648 and setPos has no effect ?! (all versions of setPos has been tested)
I am calling this function from QML but i dont think that should matter.

Running Windows 10 and Qt 5.12

Any help and ideas are appreciated.

Br
--Anders

anda_skoa
25th March 2019, 09:15
What if you pass a valid screen pointer to pos() instead of a null pointer. Or call the pos() overload without arguments?

Cheers,
_

Androiders
25th March 2019, 10:00
Good catch :) However it makes no difference with any other version of pos()

Br
--Anders

ChristianEhrlicher
25th March 2019, 21:22
https://doc.qt.io/qt-5/qcursor.html#setPos-1

"Note: On platforms where there is no windowing system or cursors are not available, this function may do nothing."

Now the question is if windows falls in this category