<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="#ffffff" text="#000000">
I need some help on how to add a context menu to a graphics item.<br>
<br>
In the sample code below I have an action (editAction) on a menu
(myMenu) that is only enabled if a item is selected.<br>
<br>
Now I would like to have this menu show up as a context menu when I
right click the rect item in the scene/view.<br>
<br>
I think the way to do this is to do my own processing of the
contextMenuEvent. But what is the best way to do that?<br>
<br>
Store the menu in the scene so that I can access it from the
graphicsItem? And then do a menu.exec in the contextMenuEvent handler?<br>
<br>
Or are they better ways to do this? Is there some route with signalling?<br>
<br>
Thanks in advance<br>
Chris<br>
<br>
<br>
<font face="Courier New, Courier, monospace" size="-1">from
PyQt4.QtCore import *<br>
from PyQt4.QtGui import *<br>
<br>
class MyRect(QGraphicsRectItem):<br>
def __init__(self, parent=None, scene=None):<br>
super(MyRect, self).__init__(parent, scene)<br>
<br>
def contextMenuEvent(self, contextEvent):<br>
print("cme")<br>
<br>
class MyView(QGraphicsView):<br>
def __init__(self, parent=None):<br>
super(MyView, self).__init__(parent)<br>
self.setMouseTracking(True)<br>
self.scale(1,1)<br>
<br>
def mouseMoveEvent(self, mouseEvent):<br>
self.emit(SIGNAL("updateCoords"),
self.mapToScene(mouseEvent.pos()))<br>
super(MyView, self).mouseMoveEvent(mouseEvent)<br>
<br>
class MyScene(QGraphicsScene):<br>
<br>
def __init__(self, parent=None):<br>
super(MyScene, self).__init__(parent)<br>
<br>
someRect = MyRect()<br>
someRect.setRect(0, 0, 160, 80)<br>
someRect.setBrush(QBrush(Qt.white, Qt.SolidPattern))<br>
someRect.setFlag(QGraphicsItem.ItemIsSelectable, True)<br>
someRect.setFlag(QGraphicsItem.ItemIsMovable, True)<br>
<br>
self.addItem(someRect)<br>
<br>
<br>
class MainWindow(QMainWindow):<br>
<br>
def __init__(self, parent=None):<br>
# call parent init<br>
super(MainWindow, self).__init__(parent)<br>
self._iconState = False<br>
<br>
# setup scene object<br>
self.scene = MyScene()<br>
<br>
# setup view object<br>
self.view = MyView()<br>
<br>
self.myAction = createAction(self, "my Action", <br>
self.testslot,<br>
Qt.Key_T,<br>
"info.ico",<br>
self.tr(" my Action comment"))<br>
<br>
self.editAction = createAction(self, "EditAction", <br>
self.testslot,<br>
Qt.Key_E,<br>
"pencil.ico",<br>
self.tr(" my EditAction comment"))<br>
<br>
self.editAction.setEnabled(False)<br>
<br>
self.testToolBar = self.addToolBar("testToolBar")<br>
self.testToolBar.setObjectName("testToolBar")<br>
<br>
self.myMenu = self.menuBar().addMenu("&TestMenu")<br>
<br>
addActions(self, self.myMenu, (<br>
self.editAction,<br>
)) <br>
<br>
addActions(self, self.testToolBar, (<br>
self.myAction,<br>
self.editAction,<br>
)) <br>
<br>
# connect scene to view<br>
self.view.setScene(self.scene)<br>
<br>
# create layout<br>
layout = QVBoxLayout()<br>
<br>
# add view to layout<br>
layout.addWidget(self.view)<br>
<br>
# set the margin of the object in the layout<br>
layout.setContentsMargins(0, 0, 0, 0)<br>
<br>
# create the central widget<br>
self.widget = QWidget()<br>
<br>
# lay it out<br>
self.widget.setLayout(layout)<br>
<br>
# set it to central<br>
self.setCentralWidget(self.widget) <br>
<br>
# setup connections<br>
self.scene.changed.connect(self.checkSelected)<br>
<br>
def checkSelected(self):<br>
if self.scene.selectedItems():<br>
self.editAction.setEnabled(True)<br>
else:<br>
self.editAction.setEnabled(False)<br>
<br>
def testslot(self):<br>
self._iconState = not self._iconState<br>
if self._iconState:<br>
self.myAction.setIcon(QIcon("icons/help.ico"))<br>
else:<br>
self.myAction.setIcon(QIcon("icons/info.ico"))<br>
<br>
def createAction(self, text, slot=None, shortcut=None, icon=None,<br>
tip=None, checkable=False, signal="triggered()"):<br>
<br>
action = QAction(text, self)<br>
<br>
if icon is not None:<br>
action.setIcon(QIcon(("icons/{}").format(icon)))<br>
if shortcut is not None:<br>
action.setShortcut(shortcut)<br>
if tip is not None:<br>
action.setToolTip(tip)<br>
action.setStatusTip(tip)<br>
if slot is not None:<br>
self.connect(action, SIGNAL(signal), slot)<br>
if checkable:<br>
action.setCheckable(True)<br>
return action<br>
<br>
def addActions(self, target, actions):<br>
for action in actions:<br>
if action is None:<br>
target.addSeparator()<br>
else:<br>
target.addAction(action)<br>
<br>
if __name__ == "__main__":<br>
<br>
import sys<br>
<br>
# setup application object<br>
app = QApplication(sys.argv)<br>
<br>
# create (parent) main window<br>
mainWindow = MainWindow()<br>
mainWindow.setWindowTitle("gridScene")<br>
mainWindow.show()<br>
<br>
# run application object<br>
sys.exit(app.exec_())</font><br>
</body>
</html>