Mouse lock inside window/widget



  • I am trying to stop the mouse from leaving the window. This is useful for first person cameras in OpenGL, as well as full screen mouse-heavy games with multiple monitors. I mostly am just looking to keep it inside the window, however keeping the mouse inside a widget could also be useful to know.



  • You can move the mouse cursor using QCursor::setPos( int x, int y );

    http://qt-project.org/doc/qt-4.8/qcursor.html#setPos



  • Why don't you just restrain the cursor to the dead center of your game window while the FPS game is running, and only release it when you open some menu and interrupt the game? It will still be useful to be able to leave your window while the game is interrupted.

    But if that is what you want to achieve, you can use the mouse leave event to restrain the cursor to its last position that was inside of the window, obviously you will have to track that.



  • Yes, What utcenter said. I've created FPS-like interaction in a QGLWidget, and using the restrain to dead-center approach worked best for me. If you let the cursor get too close to the edge, rapid subsequent mouse movements can leave the frame, resulting in sometimes missing mousemove events. And you want to avoid things like using grabMouse() to get around that if at all possible.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.