开发者

Qt mouse leave event while button is pressed

开发者 https://www.devze.com 2023-04-05 15:28 出处:网络
I need to detect if the mouse pointer leaves my custom widget even if a mouse button is pressed. According to this post, Qt does not cause a leaveEvent in case a button is pressed, at least not in ve

I need to detect if the mouse pointer leaves my custom widget even if a mouse button is pressed.

According to this post, Qt does not cause a leaveEvent in case a button is pressed, at least not in version 4.4. I am working with 4.7.3, but still do not get a leaveEvent in the described case.开发者_开发百科 I also tried with the various drag-related events, but no luck. Does anyone have an idea how to deal with this?


Actually there is an even better option than using the mouse events of the parent widget: You can implement the mouseMoveEvent function of the child widget and get the QMouseEvent::pos() position which is relative to the top left corner of the widget. That means, if you know the size of the widget (use for example QWidget::rect()), you can compute within the widget if the mouse pointer is still on the widget or not without having to change the parent widget.


Well, I did something similar to @gregseth.

Write your own MouseLeaveEvent that is invoked from MouseMoveEvent when the mouse's position is outside of the widget's boundary.

As Qt's documentation says "Mouse move events will occur only when a mouse button is pressed down"


I got the same problem. The only workaround I found is to manage the mouseMove event of the parent widget, and to check if the position of the cursor is inside or outside the boundaries of the widget you want the event on.

0

精彩评论

暂无评论...
验证码 换一张
取 消