[Qt-interest] Connecting context menu to slot in QGraphicsItem
Cole, Derek
dcole at integrity-apps.com
Thu Apr 7 23:39:58 CEST 2011
Worked like a charm - thanks!
I knew the leak was there, but I was just playing around with some code I found on forums which claimed to be correct. It explicitly had the connection after the exec, but anyway, this works.
Thanks again
Derek
-----Original Message-----
From: qt-interest-bounces+dcole=integrity-apps.com at qt.nokia.com on behalf of Ben Swerts
Sent: Thu 4/7/2011 5:29 PM
To: qt-interest at qt.nokia.com
Subject: Re: [Qt-interest] Connecting context menu to slot in QGraphicsItem
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/a4af615e/attachment.html
More information about the Qt-interest-old
mailing list