4
我有一個使用QTableWidget(PyQt)的多個表的窗口。我使用鼠標右鍵創建了一個彈出菜單,它工作正常。 但是,我需要創建不同的彈出菜單,根據鼠標點擊鼠標時鼠標懸停在哪個表上。我怎樣才能讓鼠標告訴我它懸停在哪張桌子上?基於鼠標位置的PyQt中的多個上下文菜單
,或者以另一種方式,如何實現一個方法,以具有基於鼠標位置特定的上下文菜單?
我正在使用Python和PyQt。
我的彈出式菜單開發類似下面的代碼(從Qt and context menu PedroMorgan答案):
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))
你說的是QTableWidget,但你的例子使用QTreeView。你在使用哪一個? – aukaost