Knute Knudsen
Knute Knudsen

Reputation: 1239

Context Menu in QGraphicsWidget using addAction()

I have a custom QGraphicsWidget in Qt5.4 to which I'm trying to add a context menu. In the item's init() routine I add two QAction items like this:

bool MyGraphicsWidget::init()
{
    this->addAction(new QAction("Rotate &Left", this));
    this->addAction(new QAction("Rotate &Right", this));
}

But when I try to pull up the context menu (right-clicking in Windows) nothing shows up.

The documentation seems to say I can just add the QAction items and the context menu will just work without my having to override mouse events or context menu events. I've tried changing the contextMenuPolicy in the QGraphicsView to ActionsContextMenu and the window flags on the QGraphicsWidget to ItemIsSelectable but to no avail. What am I missing here?

Upvotes: 0

Views: 386

Answers (1)

svlasov
svlasov

Reputation: 10456

You need to use contextMenuEvent with QMenu:

void contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    QMenu menu;
    menu.addAction(new QAction("Rotate &Left", this));
    menu.addAction(new QAction("Rotate &Right", this));
    menu.exec(event->screenPos());
}

Upvotes: 1

Related Questions