Results 1 to 5 of 5

Thread: Popup menu for a QGraphicsItem?

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Wink Popup menu for a QGraphicsItem?

    I have a class X that is a subclass of QGraphicsRectItem and I wiish to have a popup menu if the item is clicked with the right mouse button..

    I'm not quite sure on how to do this.

    void X::contextMenuEvent(QGraphicsSceneContextMenuEvent * event)
    {
    // if this is the proper way, how do I create a menu here
    }

    And is it defined somewhere that a rightclick generates a QGraphicsSceneContextMenuEvent ?
    Or should I write code in the MousePressEvent() handler?

    Anyway I would really like to know how to create the popup for the QGraphicsItem.

  2. #2
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Popup menu for a QGraphicsItem?

    I think I've found the problem I had. A QGraphicsItem is not a QWidget, so it seems to work now. Therefor I change the question to: what is the difference between synchronously and asynchronously execution?

  3. #3
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Popup menu for a QGraphicsItem?

    A synchronous menu call, like QMenu::exec(), enters a modal event loop, and provides easy-to-read code like in the following example:

    Qt Code:
    1. void MyItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
    2. {
    3. QMenu menu;
    4. menu.addAction("Action 1");
    5. menu.addAction("Action 2");
    6. QAction *a = menu.exec(event->screenPos());
    7. qDebug("User clicked %s", qPrintable(a->text()));
    8. }
    To copy to clipboard, switch view to plain text mode 

    You can create the menu on the stack, and the result of the menu is available immediately. This is the preferred approach for QGraphicsItem. Beware, though, that because the event loop is completely reentered by the exec() call, arbitrary events will be processed, and this can lead to unexpected results (like how do you handle incoming network data while the popup menu is shown?).

    An asynchronous call, like calling QMenu:opup(), does not reenter the event loop. Instead, you can deliver the results from the menu invocation to a slot in a QObject subclass.

    Qt Code:
    1. void MyItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
    2. {
    3. QMenu *menu = new QMenu;
    4. menu->addAction("Action 1");
    5. menu->addAction("Action 2");
    6. menu->popup(event->screenPos());
    7.  
    8. connect(menu, SIGNAL(triggered(QAction *)),
    9. object, SLOT(triggered(QAction *)));
    10. }
    To copy to clipboard, switch view to plain text mode 
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  4. The following 4 users say thank you to Bitto for this useful post:

    ajo (20th February 2013), Gopala Krishna (18th January 2007), minimoog (12th April 2010), Morea (4th February 2009)

  5. #4
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Popup menu for a QGraphicsItem?

    What is the difference between contextMenuEvent and handling mousepressEvent and checking if the rightbutton was clicked?

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Popup menu for a QGraphicsItem?

    From QContextMenuEvent docs:
    Context menu events are sent to widgets when a user performs an action associated with opening a context menu. The actions required to open context menus vary between platforms; for example, on Windows, pressing the menu button or clicking the right mouse button will cause this event to be sent.
    J-P Nurmi

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  2. ListView Dynamic Popup menu
    By raphaelf in forum Newbie
    Replies: 3
    Last Post: 14th October 2006, 19:26
  3. Can't close my popup
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2006, 09:10
  4. Background image on popup menu item
    By MarkoSan in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 27th June 2006, 05:55
  5. Replies: 6
    Last Post: 14th April 2006, 05:39

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.