我正在尝试在我的QGraphicsItem子类中实现上下文菜单,如下所示:
void ImagePixmapItem::right_clicked(){
qDebug("Got here!");
}
void ImagePixmapItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event){
QMenu menu;
QAction *removeAction = menu.addAction("Remove");
QAction *markAction = menu.addAction("Mark");
QAction *selectedAction = menu.exec(event->screenPos());
connect(selectedAction, SIGNAL(triggered()),this, SLOT(right_clicked()));
}
我做错了什么?文字“到了!”永远不会被解雇,而且,如何修改它以了解从菜单中选择了哪个动作?
由于
答案 0 :(得分:1)
离开范围时删除QMenu。
试试这个:
void ImagePixmapItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event){
std::auto_ptr<QMenu> menu(new QMenu(address_of_parent_widget));
QAction *removeAction = menu->addAction("Remove");
QAction *markAction = menu->addAction("Mark");
QAction *selectedAction = menu->exec(event->screenPos());
if ( selectedAction != 0 ) { // see: http://doc.trolltech.com/4.3/qmenu.html#exec-2
this->right_clicked();
}
}