tags:

views:

235

answers:

1

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 http://stackoverflow.com/questions/1354877/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?

+2  A: 

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 :

  1. 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.
  2. You can use the static method QCoreApplication::sendEvent (...) for doing the same job.
  3. Event filter can do the job, see Events and event filters chapter.
  4. 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 !

Matthieu
I've moved my initial grabbing of events from *View* to *Scene*; I'm closer now but still not getting my events at the bottom. QGraphicsScene::sendEvent() was added in Qt4.6; I'm still on Qt4.5. This might be the time to update. Event filter wasn't be nice to me either; I'm going to give it a new go now that I moved the layer I'm working on. Thanks for all the great suggestions! I'll post a follow-up when I nail it.
dwj
These were all good suggestions. In the end, I stayed with Qt4.5 (we have lots of run-time on this right now and don't have a pressing need to update). To solve this: Moved mouse press event capture to the Scene and then call QGraphicsScene::mousePressEvent(event); in Scene::mousePressEvent() for cases I wanted the event to continue on to the QGraphicsItem.
dwj