我试图像这样在我的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()));
}
我在做什么错?文字“在这里!”永远不会被解雇,此外,我如何修改它以知道从菜单中选择了哪个操作?
感谢
当菜单对象超出范围时,将删除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();
}
}