如何从QGraphicsItem的上下文菜单中运行功能

问题描述 投票:1回答:1

我试图像这样在我的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()));
}

我在做什么错?文字“在这里!”永远不会被解雇,此外,我如何修改它以知道从菜单中选择了哪个操作?

感谢

c++ qt4 qgraphicsitem
1个回答
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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.