2

我有一个 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)

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

谢谢您的帮助!!!

4

1 回答 1

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())
于 2013-02-20T05:03:24.970 回答