Multiple Context menus in PyQt based on mouse location
- by Nader
I have a window with multiple tables using QTableWidget (PyQt). I created a popup menu using the right click mouse and it works fine.
However, I need to create different popup menu based on which table the mouse is hovering over at the time the right mouse is clicked. How can I get the mouse to tell me which table it is hovering over?
or, put in another way, how to implement a method so as to have a specific context menu based on mouse location?
I am using Python and PyQt.
My popup menu is developed similar to this code (PedroMorgan answer from Qt and context menu):
class Foo( QtGui.QWidget ):
def __init__(self):
QtGui.QWidget.__init__(self, None)
# Toolbar
toolbar = QtGui.QToolBar()
# Actions
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)
# Tree
self.tree = QtGui.QTreeView()
self.tree.setContextMenuPolicy( Qt.CustomContextMenu )
self.connect(self.tree, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.on_context_menu)
# Popup Menu
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) )