PDA

View Full Version : QGraphicsView and contextMenuEvent



laurabee
11th October 2006, 21:46
I have a subclass of QGraphicsView and several subclasses of QGraphicsItem. I want different menus to appear when right-clicking on the different kinds of items and when right-clicking on the background of the view.

I have overrides for "void QGraphicsItem::contextMenuEvent(QGraphicsSceneCont extMenuEvent *event)" in my items and that works just fine. When I override "void QGraphicsView::contextMenuEvent(QContextMenuEvent *event)" in my view, I call QGraphicsView's implementation to propagate the event to the items.

How can I tell whether any of the items handled the event? I see this documentation for QGraphicsItem:
"If you ignore the event, (i.e., by calling QEvent::ignore(),) event will propagate to any item beneath this item. If no items accept the event, it will be ignored by the scene, and propagate to the view."

That's the behavior I want, but how is the event propagated to the view? The event has already occurred on the view (the standard QWidget contextMenuEvent). I tried checking the isAccepted flag on the QContextMenuEvent, but it seems to always be set to true once QGraphicsView's contextMenuEvent method is called.

I have a work-around, but I'd like to use the built-in event propagation, if possible.

Any ideas?

high_flyer
12th October 2006, 23:22
That's the behavior I want, but how is the event propagated to the view?
Its not.
The view is the one propagating events to the scene, which propagetes them to the items.
As you can read in QGraphicsScene doc:

Another responsibility that QGraphicsScene has, is to propagate events from QGraphicsView.
and QGraphicsView:

QGraphicsView translates the mouse and key events into scene events, (events that inherit QGraphicsSceneEvent,), and forward them to the visualized scene. In the end, it's the individual item that handles the events and reacts to them.
....
You can also provide your own custom scene interaction, by creating a subclass of QGraphicsView, and reimplementing the mouse and key event handlers.

I call QGraphicsView's implementation to propagate the event to the items.

You don't need to do that, it happens any how.
You should implement your event handlers in your items.

How can I tell whether any of the items handled the event?
You are the one deciding if an item should handle an event, and then you are resposible to have that item ignore or accept that event, thus informing the secene and view if this event was handled or not.
At least that is the way I undertood the docs.