Passing / forwarding events to a sub-object in Qt

events, qt

Solution

Is there a way to turn on/off event handling during run-time and make sure they get to the lower levels? Or is there a way to forward events to a sub-object?

There are some possibilities :

- Use QGraphicsScene::sendEvent(...) to send event to items. As the scene() is accessible from any items by the method scene(). Maybe it's a solution for you.

- You can use the static method QCoreApplication::sendEvent (...) for doing the same job.

- Event filter can do the job, see Events and event filters chapter.

- You can use specific install event system of Graphicsview framework (QGraphicsItem::sceneEventFilter(...) and installSceneEventFilter(...)

At runtime, it's possible to turn off/on mouse event with the method QGraphicsItem::setAcceptedMouseButtons(...).

There are many other possibilities...

Hope it helps !

Problem

I'm working on a Qt application and I have a problem with getting mouse events where I want them. Here's a high-level view of what I have (there are other things going on at each level that dictate the need for the views and scenes): ``` +---------------- | App Window -- QMainWindow | +-------------- | | View -- QGraphicsView --- Grabbing mouse events here for Mode 1 | | +------------ | | | Scene -- QGraphicsScene | | | +---------- | | | | Image -- QGraphicsPixmap --- Want to grab mouse events here for Mode 2 | | | | ``` In Mode 1, I'm grabbing the event mousePressEvent at the View level. In Mode 2, I'd like to grab the mouse events at the Image level. Before I overloaded mousePressEvent in the View class I was able to get all events in Image. Now that I'm capturing at View I can't get mouse events in Image. Image is private within Scene and I don't want to expose it. There is no inheritance here; each item is an object created by the object above it. Reading through this question Qt -- pass events to multiple objects? makes it seem like I want to capture mouse events at the Image level and let them go back up; however, I would still need to create mousePressEvent in View and I'd be right back where I am. Is there a way to turn on/off event handling during run-time and make sure they get to the lower levels? Or is there a way to forward events to a sub-object?

Original source