I need to create a context menu on right clicking at my window. But I really don\'t know how to achieve that.
Are there any widgets for that, or I have to create it
Another example which shows how to use actions in a toolbar and context menu.
class Foo( QtGui.QWidget ):
def __init__(self):
QtGui.QWidget.__init__(self, None)
mainLayout = QtGui.QVBoxLayout()
self.setLayout(mainLayout)
# Toolbar
toolbar = QtGui.QToolBar()
mainLayout.addWidget(toolbar)
# Action are added/created using the toolbar.addAction
# which creates a QAction, and returns a pointer..
# .. instead of myAct = new QAction().. toolbar.AddAction(myAct)
# see also menu.addAction and others
self.actionAdd = toolbar.addAction("New", self.on_action_add)
self.actionEdit = toolbar.addAction("Edit", self.on_action_edit)
self.actionDelete = toolbar.addAction("Delete", self.on_action_delete)
self.actionDelete.setDisabled(True)
# Tree
self.tree = QtGui.QTreeView()
mainLayout.addWidget(self.tree)
self.tree.setContextMenuPolicy( Qt.CustomContextMenu )
self.connect(self.tree, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.on_context_menu)
# Popup Menu is not visible, but we add actions from above
self.popMenu = QtGui.QMenu( self )
self.popMenu.addAction( self.actionEdit )
self.popMenu.addAction( self.actionDelete )
self.popMenu.addSeparator()
self.popMenu.addAction( self.actionAdd )
def on_context_menu(self, point):
self.popMenu.exec_( self.tree.mapToGlobal(point) )