Results 1 to 4 of 4

Thread: Context menus for GraphicsView and QGraphicsItem

  1. #1
    Join Date
    Feb 2016
    Posts
    2

    Default Context menus for GraphicsView and QGraphicsItem

    There is a GraphiscView widget in my dialog, and menu setup is looking:

    Qt Code:
    1. m_ui->graphicsView->setContextMenuPolicy(Qt::CustomContextMenu);
    2. connect(m_ui->graphicsView, SIGNAL(customContextMenuRequested(const QPoint&)),
    3. this, SLOT(showContextMenu(const QPoint&)));
    To copy to clipboard, switch view to plain text mode 

    If I add an object of a class, derived from QGraphicsItem, and having this method overriden:

    Qt Code:
    1. void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) Q_DECL_OVERRIDE;
    To copy to clipboard, switch view to plain text mode 

    , the graphicsView context menu is beig shown, if I right-click on it, not one of the item.
    Item's menu is shown only if I comment out set up of graphicsView menu.

    How to do it right way, that if I click on an item, its menu is shown, if on an empty field of graphicsView - the graphicsView's menu?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Context menus for GraphicsView and QGraphicsItem

    Look at the example code in QGraphicsView::itemAt(). You will need to implement something like that in your showContextMenu() slot - first check to see if there is an item at the QPoint. If there is, and it is one of your custom items, then you need to display its context menu (maybe send it a QGraphicsSceneContextMenuEvent). If there is nothing at the QPoint, then you display the QGraphicsView's context menu. The problem you are having is that the view's context menu handler is eating the right-clicks and your graphics items never see them.

  3. #3
    Join Date
    Feb 2016
    Posts
    2

    Default Re: Context menus for GraphicsView and QGraphicsItem

    Thank you, d_stranz!
    You mean, I should "propagate" this event myself, qt framework does not do it? Then a question: why to implement something concerning menu in the item? Maybe, if only graphicsview knows about this event, and it shows a menu, it should process the rest? I do not like it, but it seems, there is an unconvenient concept in the framework.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Context menus for GraphicsView and QGraphicsItem

    The QGraphicsItem is a child of the QGraphicsScene, not the QGraphicsView. Therefore, it has its own mechanism for handling context events. If you put the scene in a QGraphicsView that does not have custom context menus, then I think the QGraphicsItem context menus will work as you expect. If you put the same scene in a QGraphicsView that does have custom context menus, then the QGraphicsView context menu handling takes precedence.

Similar Threads

  1. Right-click context menus freeze on Windows
    By E099844 in forum Qt Programming
    Replies: 1
    Last Post: 25th October 2010, 17:35
  2. QGraphicsItem and Context Menus
    By SixDegrees in forum Qt Programming
    Replies: 2
    Last Post: 31st August 2010, 21:50
  3. Context Menus
    By qtoptus in forum Newbie
    Replies: 0
    Last Post: 25th June 2010, 15:18
  4. Context Menus
    By rodlbr in forum Qt Programming
    Replies: 4
    Last Post: 7th September 2009, 16:30
  5. Multiple Context Menus
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 24th December 2008, 19:55

Tags for this Thread

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.