Results 1 to 4 of 4

Thread: Can not invoke contextMenuEvent in QGraphicsView

  1. #1
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    9
    Qt products
    Qt4

    Default Can not invoke contextMenuEvent in QGraphicsView

    Hi experts,

    I code a TabletCanvas inherited from QGraphicsView, and overwrite contextMenuEvent() to bring the item to front in the scene.

    However, right click on TabletCanvas cant invokes contextMenuEvent(),
    even I disable all mouse events.

    Can you tell me what's wrong with it ? Thank your review and answer.


    Qt Code:
    1. class TabletCanvas : public QGraphicsView {
    2. ..
    3.  
    4. TabletCanvas()
    5. {
    6. zorderAct= new QAction(tr("bring to front"), this);
    7. connect(zorderAct, SIGNAL(triggered()), this, SLOT(itemZorder()));
    8. addAction(zorderAct);
    9. }
    10.  
    11. virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
    12. {
    13. QMenu menu(this);
    14. menu.addAction(zorderAct);
    15. menu.exec(event->screenPos());
    16. }
    17.  
    18. //virtual void mouseMoveEvent(QMouseEvent *event);
    19. //virtual void mousePressEvent(QMouseEvent *event);
    20. //virtual void mouseReleaseEvent(QMouseEvent *event);
    21. //virtual void mouseDoubleClickEvent(QMouseEvent *event);
    22. };
    To copy to clipboard, switch view to plain text mode 

  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: Can not invoke contextMenuEvent in QGraphicsView

    Commenting out mouse event handlers does not disable them. It simply means that they are processed by the QGraphicsView event handlers instead. Why don't you read the documentation for QWidget::contextMenuEvent() and maybe you will discover why you are not seeing context menu events in your widget.

  3. The following user says thank you to d_stranz for this useful post:

    chiaminhsu (29th April 2013)

  4. #3
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    9
    Qt products
    Qt4

    Default Re: Can not invoke contextMenuEvent in QGraphicsView

    Hi d_stranz,

    Thank your suggestion.

    I review the document you said.
    Then I set the context menu policy as Qt::ActionsContextMenu
    and DO NOT overwrite the contextMenuEvent().

    It works

    Qt Code:
    1. TabletCanvas::TabletCanvas()
    2. {
    3. QAction* zorderAct= new QAction(tr("bring to front"), this);
    4. connect(zorderAct, SIGNAL(triggered()), this, SLOT(itemZorder()));
    5. addAction(zorderAct);
    6. [COLOR="#FF0000"]setContexMenuPolicy(Qt::ActionsContextMenu);[/COLOR]
    7. }
    To copy to clipboard, switch view to plain text mode 

    Although I still not understand why Qt:eaultContextMenu and overwrite contextMenuEvent can not work.
    It should be another solution in the document

  5. #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: Can not invoke contextMenuEvent in QGraphicsView

    Although I still not understand why Qt:: DefaultContextMenu and overwrite contextMenuEvent can not work.
    It should be another solution in the document
    Because in your original code, you did not overwrite contextMenuEvent( QContextMenuEvent * ). You created a method called void contextMenuEvent( QGraphicsSceneContextMenuEvent * ), which is not the same as void contextMenuEvent( QContextMenuEvent * ) and so it does not match the signature required for the event handler. Change the argument to your event handler definition, and try that. Inside the event handler, you can qobject_cast<QGraphicsSceneContextMenuEvent *>( event ) to get the QGraphicsSceneContextMenuEvent pointer:

    Qt Code:
    1. // TabletCanvas.h
    2.  
    3. class TabletCanvas : public QGraphicsView
    4. {
    5. //...
    6.  
    7. protected:
    8. void contextMenuEvent( QContextMenuEvent * event );
    9. // ...
    10. };
    11.  
    12. // TabletCanvas.cpp
    13.  
    14. TabletCanvas::TabletCanvas( QWidget * parent )
    15. : QGraphicsView( parent )
    16. {
    17. setContextMenuPolicy( Qt:DefaultContextMenu );
    18. // ...
    19. }
    20.  
    21. void TabletCanvas::contextMenuEvent( QContextMenuEvent * event )
    22. {
    23. QGraphicsSceneContextMenuEvent * sEvent = qobject_cast< QGraphicsSceneContextMenuEvent * >( event );
    24. if ( sEvent )
    25. {
    26. // Have fun
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Frozencolumn contextMenuEvent
    By cia.michele in forum Qt Programming
    Replies: 0
    Last Post: 18th April 2011, 08:44
  2. contextMenuEvent clashing
    By prashant in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2009, 17:07
  3. QTableView contextMenuEvent
    By derrickbj in forum Qt Programming
    Replies: 1
    Last Post: 1st March 2007, 16:37
  4. QGraphicsView and contextMenuEvent
    By laurabee in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2006, 22:22
  5. contextMenuEvent
    By mickey in forum Qt Programming
    Replies: 13
    Last Post: 3rd June 2006, 14:14

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.