pyqt - QGraphicsScene的contextmenu

时间:2013-01-26 09:16:28

标签: python contextmenu pyqt4 qgraphicsscene

我有一个QGraphicsScene,我将显示一个上下文菜单。但没有任何事情发生。

这是我的代码:

class graphicsScene(QtGui.QGraphicsScene):
  def __init__ (self, parent = None):
    super(graphicsScene, self).__init__ (parent)

  def contextMenuEvent(self, event):
    self.popMenu = QtGui.QMenu()
    self.popMenu.addAction(QtGui.QAction('test0', None))
    self.popMenu.addAction(QtGui.QAction('test1', None))
    self.popMenu.addSeparator()
    self.popMenu.addAction(QtGui.QAction('test2', None))
    self.popMenu.exec_(event.globalPos())

  def mousePressEvent(self, event):
    super(graphicsScene, self).mousePressEvent(event)
    pos = event.scenePos()
    item = self.itemAt(pos)

    if event.button() == QtCore.Qt.LeftButton:
      #do something

    elif event.button() == QtCore.Qt.RightButton:
      self.contextMenuEvent(event)

我不知道如何解决这个问题。

感谢您的帮助!!!

1 个答案:

答案 0 :(得分:1)

这可能有所帮助:

class Table(QtGui.QGraphicsView):
  def __init__(self, parent=None):
    super(Table, self).__init__(parent)
    self.setScene(QtGui.QGraphicsScene())
    self.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)

  def contextMenuEvent(self, event):
    menu = QtGui.QMenu()
    menu.addAction('sample')
    menu.exec_(event.globalPos())