Question

I'm working in a classic QgraphicsView / QGraphicsScene / QGraphicsItem framework. I'm declaring a context menu in the QgraphicsView:

    self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
    self.customContextMenuRequested.connect(self.__contextMenu)

and then later on:

# ---------------------------------------------------------------------------
def __contextMenu(self, position):
  """ """
  # ----> Creating Context menu -----------------------------------
  oTreeMenu = QtGui.QMenu()

  etc ...

then in the QGraphicsItem instancing class I use the contextMenuEvent like the following:

# ---------------------------------------------------------------------------
def contextMenuEvent(self, event):
  """ """
  # ----> Creating Context menu -----------------------------------
  oTreeMenu = QtGui.QMenu()

The problem being that the QGraphicsItem event is completely overriden by the QGraphicsView's. How should I proceed to get both of them ?

Was it helpful?

Solution 2

Ok,I found a solution:

I'm using the QGraphicsScene contextMenuEvent instead of the 2 others. Within the event I'm checking whether the mouse is above a QGraphicsItem or not, and I build the corresponding menu. I dont really like this solution as all my functions will be under the QGraphicsScene class and most of them will be concerning the item, not the scene. So it's kinda messy, but it works. Thanks in advance to anyone who has a better solution.

OTHER TIPS

I did it in C ++ but I think it should help

In GraphicsView:

void MyGraphicsView::contextMenuEvent(QContextMenuEvent * event)
{
    QGraphicsView::contextMenuEvent(event);
    if(!event->isAccepted())
    {
        QMenu * menu  = new QMenu;
        menu->addAction("GraphicsView Action 1");
        menu->addAction("GraphicsView Action 2");
        menu->popup(event->globalPos());
    }
}

In GraphicsItem:

void MyGraphicsItem::contextMenuEvent(QGraphicsSceneContextMenuEvent * event)
{
    QMenu *menu = new QMenu;
    menu->addAction("Action 1");
    menu->addAction("Action 2");
    menu->popup(event->screenPos());
    event->setAccepted(true);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top