Code example. I don't guarantee that copy-paste will work as It is only a part of my whole code, but it should give you some tips and ideas.
import pyqtgraph as pg
class MyCustomPlotContextMenu(pg.ViewBox):
"""Subclass of ViewBox.
"""
def __init__(self parent=None):
"""Constructor of the QVizCustomPlotContextMenu
super(MyCustomPlotContextMenu, self).__init__(parent)
# Set original plot context menu
# Note: self.menu must not be None (this way works fine for plotWidgets,
# but not for GraphicsWindow)
self.menu = pg.ViewBoxMenu.ViewBoxMenu(self)
# Menu update property
self.menuUpdate = True
def getMenu(self, event=None):
"""Modify the menu. Called by the pyqtgraph.ViewBox raiseContextMenu()
routine.
Note: Overwriting the ViewBox.py getMenu() function.
"""
if self.menuUpdate is True:
# Modify contents of the original ViewBoxMenu
for action in self.menu.actions():
# Modify the original Mouse Mode
if "Mouse Mode" in action.text():
# Change action labels
for mouseAction in self.menu.leftMenu.actions():
if "3 button" in mouseAction.text():
mouseAction.setText("CustomLabel1")
elif "1 button" in mouseAction.text():
mouseAction.setText("CustomLabel2")
# Add custom contents to menu
self.addCustomToMenu()
# Set menu update to false
self.menuUpdate = False
return self.menu
def addCustomToMenu(self):
"""Add custom actions to the menu.
"""
self.menu.addSeparator()
# Autorange feature
self.actionAutoRange = QAction("Auto Range", self.menu)
self.actionAutoRange.triggered.connect(self.autoRange)
# - Add to main menu
self.menu.addAction(self.actionAutoRange)
# - Add to main menu
self.menu.addAction(self.actionConfigurePlot)