Irbis
Irbis

Reputation: 13369

Qt - setting relative mouse position

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 ?

Upvotes: 6

Views: 9285

Answers (1)

Frank Osterfeld
Frank Osterfeld

Reputation: 25155

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);

Upvotes: 13

Related Questions