PDA

View Full Version : How to reliably detect if the mouse is inside a widget



Cruz
29th December 2011, 21:48
Hello!

The title says it all, I want to detect if the mouse pointer is inside a widget or not. Mouse tracking is enabled and I get mouseMoveEvents when the mouse is moving inside the widget. However, I do not get an event when the mouse is outside of the widget, so I can't detect if the mouse is actually outside or just not moving.

I tried a naive approach by defining a margin along the border of the widget and if the mouse pointer is in that margin I assume that the mouse has left the widget. But any margin can be jumped over if the mouse is moving fast enough so this was a waste of time.

How to do it right?

mvuori
1st January 2012, 22:48
I don't know the right way, but I guess you can get the global cursor position with QCursor::pos()
( http://developer.qt.nokia.com/doc/qt-4.8/qcursor.html#pos ) and then see whether it is inside your widget.

wysota
1st January 2012, 23:22
See QWidget::enterEvent() and QWidget::leaveEvent().