I'm moving my graphics engine from Freeglut to Qt. My window class inherits from QWindow. I have a problem with setting relative mouse position to the center of the window and hiding a cursor. In freeglut the code looks like this:
glutWarpPointer((glutGet(GLUT_WINDOW_WIDTH) / 2), (glutGet(GLUT_WINDOW_HEIGHT) / 2));
glutSetCursor(GLUT_CURSOR_NONE);
I was trying something like this:
this->cursor().setPos((width() / 2), (height() / 2)); // this seems to set an absolute (global) position
this->cursor().setShape(Qt::BlankCursor); // doesn't work
How to achieve that ?
Your code doesn't have any effect, because you're editing a temporary copy.
Look at at the signature: QCursor QWidget::cursor() const
. The cursor object is returned by value.
To apply cursor changes, you must pass the modified object back via setCursor()
. To map from local to global coordinates, use mapToGlobal()
:
QCursor c = cursor();
c.setPos(mapToGlobal(QPoint(width() / 2, height() / 2)));
c.setShape(Qt::BlankCursor);
setCursor(c);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With