Results 1 to 1 of 1

Thread: QGraphicsItem deselected on contextMenuEvent

  1. #1
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    4
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QGraphicsItem deselected on contextMenuEvent

    Hi,

    I have subclassed QGraphicsScene and want to reimplement the QGraphicsScene::contextMenuEvent(QGraphicsSceneCon textMenuEvent *) function. My plan was to catch all context events with the scene and dynamically create a context menu depending on the combination of types of QGraphicsItems selected. I want the menu to only contain actions that all selected items will support. I started like this:

    Qt Code:
    1. void MyScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent)
    2. {
    3. QList<QGraphicsItem *> selected = selectedItems(); // Always returns an empty list
    4. // Create context menu dynamically here and execute it
    5. }
    To copy to clipboard, switch view to plain text mode 

    The problem I am encountering is that a right click context menu event deselects all items in the scene. This bug is documented here. I need some sort of workaround, but am struggling with this one. Anyone have any ideas? Thanks in advance.

    Hmmm...I found a solution. The Qt docs for QGraphicsScene::mousePressEvent(...) state:
    If there is no item at the given position on the scene, the selection area is reset, any focus item loses its input focus, and the event is then ignored.
    I had reimplemented this function, but wasn't accepting anything but left mouse button events. Accepting right click mouse button events solved the problem:

    Qt Code:
    1. void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. if (event->button() != Qt::LeftButton) {
    4. event->accept();
    5. return;
    6. }
    7. QGraphicsScene::mousePressEvent(event);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by D Cell; 14th December 2010 at 06:23.

  2. The following 4 users say thank you to D Cell for this useful post:

    maxpyne (19th April 2016), nilot (15th February 2017), Sintegrial (19th September 2016), veiovis (6th July 2015)

Similar Threads

  1. contextMenuEvent clashing
    By prashant in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2009, 18:07
  2. Use ActionsContextMenu or contextMenuEvent
    By grabnerj in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2008, 07:44
  3. contextMenuEvent in QGraphicsItem
    By popai in forum Qt Programming
    Replies: 2
    Last Post: 17th August 2007, 10:13
  4. QGraphicsView and contextMenuEvent
    By laurabee in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2006, 23:22
  5. contextMenuEvent
    By mickey in forum Qt Programming
    Replies: 13
    Last Post: 3rd June 2006, 15: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.