Handle mouse events in QGrahpicsItem but ignore mouse move events - qt

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.

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.

Qt Event process order in scene items model

I custom my own item, which inherits from QGraphicsItem, override the mousePressEvent function, then I add it into QGraphicScene.
When I debug, I move mouse onto my item, then press down, I find that the QGraphicScene's event processing function is called first(I install a event processing filter on QGraphicScene), then mousePressEvent of my custom item is called, is this correct?
How can I made my custom item earlier receiver than QGraphicScene?
The behavior you describe is correct and by design. The item will never receive the event before the scene does; it is - after all - the scene's job to route the event to the correct item. The scene maintains spatial index of the items and uses it to route the event. What you can do instead is to filter the event on the item itself, not on the scene: use QGraphicsItem::installSceneEventFilter.

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 capture mouse events on an arbitrary QWidget?

I'm building a GUI with Python and Qt. The standard widgets are quite useful and work out of the box. However, I have some ideas about mouse gestures. More precisely, a button or label or text, that, after being clicked ant the mouse held pressed, moving the mouse around around has special effects.
What is necessary to add mouse support for the following events
mouse clicked over widget A while it was visible
mouse moved to x, y (at real time)
mouse released
to an arbitrary widget?
Right now I am trying to do this by class A, which inherits QAbstractItemView and owns a QWidget. However, nothing works AND
NotImplementedError: QAbstractItemView.verticalOffset() is abstract and must be overridden
QAbstractItemView is not helpful for your task.
You can install event filter on an arbitrary widget using installEventFilter. Your filter class must be inherited from QObject. The documentation contains some useful examples. See QObject::installEventFilter. If you want to install filter on all widgets at once, you can install it for QApplication instance.
Another option is to subclass QWidget (or any other QWidget-derived class) and reimplement its mousePressEvent, mouseMoveEvent and mouseReleaseEvent virtual functions.

Qt: Can mouse event handlers block one another?

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.

Resources