Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - setting relative mouse position

Tags:

qt

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 ?

like image 438
Irbis Avatar asked Oct 31 '13 18:10

Irbis


Video Answer


1 Answers

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);
like image 87
Frank Osterfeld Avatar answered Sep 30 '22 14:09

Frank Osterfeld