Results 1 to 2 of 2

Thread: QGraphicsView and contextMenuEvent

  1. #1
    Join Date
    Oct 2006
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsView and contextMenuEvent

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView and contextMenuEvent

    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.
    Last edited by high_flyer; 12th October 2006 at 22:40.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.