Qt: Can mouse event handlers block one another? - qt

I have a simple parent widget that reimplements mousePressEvent/mouseReleaseEvent. The parent's child widgets use enterEvent/leaveEvent. When I hover the mouse over the child widgets, leaveEvent/enterEvent executes, but when I click and hold the mouse, mousePressEvent executes, but enterEvent/leaveEvent goes silent (in other words, no click and drag). Any ideas about what could be causing this?

If you press and hold down the mouse button on a widget then that widget grabs mouse events until you release the button. This is not a special feature of Qt, you can find similar behaviour in every other GUI APIs I know.
Take a look at the relevant part of the Qt documentation:
QWidget / Events:
mousePressEvent() is called when a mouse button is pressed while the
mouse cursor is inside the widget, or when the widget has grabbed the
mouse using grabMouse(). Pressing the mouse without releasing it is
effectively the same as calling grabMouse().
void QWidget::grabMouse ():
Grabs the mouse input. This widget receives all mouse events until
releaseMouse() is called; other widgets get no mouse events at all.

Related

How can child widget capture the mouse event under such circumstance

I need to simulate drag and drop in Qt, but I'm facing a problem. Here is the simplified version of the problem:
here you can see the action I took. I pressed the mouse in the area of parent widget and moved it to a child widget, keeping pressing, I did not release the mouse until it is moved into the child Widget. However, I found that the child widget could not capture the mouse release event when I released the mouse. (Actually, I found it was not only mouse release event, but also any other mouse event which was supposed to happen when the mouse was in the child widget)
Could someone give me a simple solution to do this? Maybe It is because the widget structure in my project is quite complicated so there is something wrong stops the transfer of the mouse event.
Try to accept mouse release event in parent widget, calculate mouse position, compare to child widget, and notify child widget Qt - Determine absolute widget and cursor position.
Or use QDrag and QMimeData if you want to pass data.

Handle mouse events in QGrahpicsItem but ignore mouse move events

I have a subclass of QGraphicsItem inside a QGraphicsScene. To handle mouse events inside the item, I reimplemented mousePressEvent and mouseReleaseEvent. Since I want the item to be movable, I wrote setFlags(ItemIsMovable); inside my constructor. But when I move the item, my mouseXxxEvent methods are called as well.
How can I detect whether the mouse event was used to move the item or not? I would be ok to only reimplement one of the above mentioned functions.

Remap context menu call on qwidget

I have my custom widget inherited from QWidget, and I've connected the widget's menu-calling signal to my slot.
connect(m_ontologyView, SIGNAL(customContextMenuRequested(QPoint)), SLOT(showContextMenuSlot(QPoint)));
Now I want user to be able to change button calling the context menu. Normally it's called with right mouse button, but how do I tell the widget to call the menu with a button of my choice?
I'm on Qt 5.4.0
Instead of using QWidget::customContextMenuRequested, you will need to reimplement the widgets mouse event functions, QWidget::mousePressEvent, QWidget::mouseReleaseEvent and QWidget::mouseMoveEvent. Inside of these events, you can then show you menu using QMenu::popup. (The point can be extracted from the mouse events).

How to check if the mouse has really left the QWidget?

Consider a QWidget window, what event is triggered when the mouse has left this window?
The window has QLineEdit fields on it and they have completers (QCompleter) for input suggestions. The actual goal is to make such an (open) completer disappear when the mouse leaves the window. This is mostly because in some environments, moving the mouse over a different window may focus that other window, but keyboard events are still sent to the QLineEdit field (even though its parent window isn't focused anymore), which is confusing.
I could implement QWidget::leaveEvent(QEvent *event) (in the window), find the currently shown completer popup and hide it, that closes the popup. But ironically, leaveEvent() is also triggered when the mouse is moved over that popup - hiding it (making it impossible to click on an item in that popup). I guess that makes sense because the popup is a different QWidget, even though the popup is indirectly owned by the window.
So how do I check if the mouse has actually left the QWidget window?
Reimplement QWidget::leaveEvent(QEvent *event) in your derived class but start by checking that rect().contains(mapFromGlobal(QCursor::pos())) is true.
If not, return without doing anything.
This should filter out all the events where the mouse is still over your widget.
Hope it helps!

In flex, how to trigger mouseevent when the focus is on TextInput?

In flex, I am using the following:
mx:TextInput mouseOver="tester(event)"
It works fine. My pointer goes over the textInput and it calls the function. But when I click inside the textInput to enter some text( focus is on the textInput) and then move the mouse (not taking mouse pointer outside of the boundary of textinput), the mouseover event is not trigerred.
If I use click event, then even if I am entering text ( or the focus is on the textinput) and then click, it will call the function.
How can I call the tester function on mouseover when the focus is on textInput?
The mouseOver event fires when the mouse is moved over the control.
Maybe you want to try the mouseMove event which will fire every time the mouse moves?
Keep in mind that mouseEvents and focusEvents are not inherently related. I would expect the mouseOver event to fire when the mouse rolls over your textInput regardless of whether or not that textInput has the focus.

Resources