PDA

View Full Version : QEvent::MouseButtonRelease with QEvent::Leave



qtstarter121
28th July 2017, 15:21
I have multiple widgets for which I want to implement the following functionality:

If the mouse hovers away from the widget, and then a mouse click occurs anywhere outside the widget , then I have an animation that hides the widget(s).

I am trying to figure out how to have two subsequent events be captured and handled in the simplest, most efficient way. I believe I'd need to use a combination of QEvent::MouseButtonRelease with QEvent::Leave but

I'm just not sure how to synchronize both since each event will be received at its own time in my event filter. So for the second event QEvent::MouseButtonRelease, how can we know when that event is handled that a previous event QEvent::Leave

has just occured to get the desired behaviour?

I am also open to any other methods that are more ideal to solving this problem.

Thanks.

high_flyer
31st July 2017, 11:53
If the mouse hovers away from the widget, and then a mouse click occurs anywhere outside the widget , then I have an animation that hides the widget(s).

This description is not very clear as it leaves open questions:
Hovering over widgets is something you generally do when the mouse moves on the screen.
So chances are that a situation where the mouse has hovered away from a widget it usually present - unless your application window opened not under the mouse pointer, and you have not yet moved the mouse over your application window.
This in turn means that usually any click will mean hiding the widgets.
More over, what if you hover over one widgets, leave it, hover over another widget and leave that as well, and then click somewhere.
Should both hovered and left widgets be hidden?

For the sake of simplicity I will assume that the answer is yes, that for each hovered and left widget when an "external" click occurs they all should be hidden.
One way to implement it would be to "register" each widget which gets a leave event (say, add its pointer to a list) and then in the mouse release event simply go over that list and call hide() on the elements in that list (and then empty the list).
More details on your part might change that answer however.

qtstarter121
1st August 2017, 22:26
Thank you for your insightful response. I apologize for the lack of clarity. The situation is as follows:

Note: My process of hiding/showing widgets is not by actually hiding/showing them , but rather it is a sliding animation of the positions of these widgets.

This is how it should work:

1) My mouse hovers to edges of the screen : left, right top, bottom, and consequently a widget "shows" up.
2) Leaving this widget and subsequently entering a second widget by hovering over to another screen edge should result in the previous widget to be "hidden" ( this is without clicking anywhere outside ).
3) Otherwise if you do not enter a second widget and simply click outside the widget somewhere, then that widget should "hide".

Based on this, I attempted a solution by registering the most recently left widget, and hiding it when a second widget is entered or when an external mouse click occurs.

I am having some difficulty where my hiding animation does not take place if I hover to an edge and a widget appears, and I click outside ( so as in case 3) above) , but rather the widget actually hides instead due to loss of focus because I have clicked inside the widget before clicking elsewhere. If I do not give my widget focus before clicking elsewhere then my hiding animation occurs correctly.

I believe I would need to override the functionality where a widget is hidden due to loss of focus and replace that code with my hiding animation. Would you be able to help me figure out what to do to achieve this?

Added after 1 50 minutes:

I read that I could override QWidget::focusOutEvent(QFocusEvent *event) for my object but the problem is that there is no separate class implementation for the widgets I need to "hide" so I think I would to somehow override
this event in my eventFilter of my QMainWindow which all the widgets are loaded into. Any help would be appreciated

high_flyer
2nd August 2017, 14:19
I read that I could override QWidget::focusOutEvent(QFocusEvent *event) for my object but the problem is that there is no separate class implementation for the widgets I need to "hide"
What is the problem with sub classing QWidget to allow the functionality you need?

I think I would to somehow override this event in my eventFilter of my QMainWindow which all the widgets are loaded into.
That too is an approach that could work.
It is not as clean as sub classing, but it can work.


Any help would be appreciated
Not sure help with what?
It would help if you point to what it is you need help with.
Do you need help with using eventFilter()? With subclassing?