[Qt-interest] Connecting context menu to slot in QGraphicsItem

Ben Swerts benswerts at telenet.be
Thu Apr 7 23:29:09 CEST 2011


Hi Derek,
 
The chosen action is returned by menu->exec. So just compare selectedAction
to removeAction and markAction to know which one was chosen.
 
It might be possible that signals are emitted but then you do the connect
call too late. You'll need to move it before the menu->exec call (This is
the point at which the menu is actually shown).
 
One final remark: you have a leak. Allocate the QMenu on the stack to
prevent this.
 
Hope it helps,
 
 
    Ben 
 



Hello,

I have created a QGraphicsItem subclass, and I am trying to add a
right-click popup context menu to it.

I have the following:

void ImagePixmapItem::right_clicked(){
    qDebug("Got here!");
}

void ImagePixmapItem::contextMenuEvent(QGraphicsSceneContextMenuEvent
*event){
    QMenu *menu = new QMenu();
    QAction *removeAction = menu->addAction("Remove");
    QAction *markAction = menu->addAction("Mark");
    QAction *selectedAction = menu->exec(event->screenPos());

    connect(menu, SIGNAL(triggered(QAction *)),this, SLOT(right_clicked()));

}

This compiles, and does not throw any errors at runtime, but I never get the
line "Got Here!" printed. What am I doing incorrectly to fire off an action
based on a selection from a context menu?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110407/bd1f86b0/attachment.html 


More information about the Qt-interest-old mailing list