Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing / forwarding events to a sub-object in Qt

Tags:

events

qt

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?

like image 517
dwj Avatar asked Jan 28 '10 23:01

dwj


1 Answers

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 !

like image 144
Matthieu Avatar answered Nov 15 '22 06:11

Matthieu