dfetter88
dfetter88

Reputation: 5391

Finding current mouse position in QT

This is my first attempt at writing a QT app, and I'm just trying to get a feel for how it works. My goal is to have a 400x400 widget which knows the exact position of the mouse when the mouse is hovering over it. For example, if the mouse was hovering in the top left corner, the position might be 10,10 (or something similar). If the mouse is in the bottom right corner, it might say 390,390.

Eventually, these coordinates will be displayed in a label on the main window, but that should be trivial. I'm stuck at the actual fetching of the coordinates. Any ideas?

Upvotes: 2

Views: 13085

Answers (2)

Zsolt Szatmari
Zsolt Szatmari

Reputation: 1239

If you are ever in a situation when you don't need actual tracking, just position at the moment, you can use QCursor::pos().

Upvotes: 6

user124493
user124493

Reputation:

For your widget, you must enable mouse tracking.

Then, you can either install an event filter, paying attention to mouse events and looking for the move event, or you can inherit from QWidget and override the mouse event, looking for mouse move events.

http://doc.qt.io/qt-4.8/qwidget.html#mouseTracking-prop

http://doc.qt.io/qt-4.8/eventsandfilters.html

http://doc.qt.io/qt-4.8/qmouseevent.html

Upvotes: 5

Related Questions