Reputation: 146
Can anyone help me to disable the context menu of pyqtgraph or remove some options from it?
Upvotes: 4
Views: 5660
Reputation: 51
In practice to implement Elliot's answer I would subclass Viewbox
, after the initialization you can hide the action of the default ViewBoxMenu
which you don't want to use. Then you can add your own actions to the self.menu
.
Below I hide the "X-axis" option and add a submenu to the default ViewBox
contextual menu to change the background color.
plot = pg.PlotWidget(title="New Menu",viewBox=NewViewBox())
class NewViewBox(pg.ViewBox):
def __init__(self,parent=None):
super(NewViewBox, self).__init__(parent)
self.myMenuEdit()
def myMenuEdit(self):
#Hide Default Actions
MenusToHide = ["X axis"] #Names of menus to hide
w = self.menu.actions()
for m in w:
for mhs in MenusToHide:
if (m.text().startswith(mhs)):
m.setVisible(False)
break
#AddMySubMenu
leftMenu = self.menu.addMenu("Background color")
group = QtGui.QActionGroup(self)
Yellow = QtGui.QAction(u'Yellow', group)
Red = QtGui.QAction(u'Red', group)
leftMenu.addActions(group.actions())
Yellow.setCheckable(True)
Red.setCheckable(True)
group.triggered.connect(self.setBgColor)
self.bgActions=[Yellow,Red]
def setBgColor(self, action):
mode = None
if action == self.bgActions[0]:
self.setBackgroundColor("y")
elif action == self.bgActions[1]:
self.setBackgroundColor("r")
Note that each parent of the ViewBox
which has a method getContextMenus
has its own menu options and they will be loaded and added to the menu at each click through the call of self.scene().addParentContextMenus(self, menu, ev)
in the function raiseContextMenu
.
PlotItem
, for example, adds Export and Plot Options. You can override the menus of each of those parental classes if needed.
Upvotes: 1
Reputation: 351
I've been working on this myself and this is what I've found (as of May 2021). For reference, the image below is the right click menu that appears for the PyQtGraph PlotWidget, but not all of the items (QActions) are added by the PlotWidget itself.
The menu items above the separator are created by the ViewBox menu (source code here) which can be accessed through PlotItem.vb
or PlotItem.getViewBox()
(if you have a PlotWidget
, you can get the PlotItem
through PlotWidget.getPlotItem()
). The menu items (QAction
s) can be accessed through PlotItem.vb.menu.actions()
(Qt reference here) and can be removed by checking the QAction.text()
to find the QAction
you want to change or remove.
PlotItem
(source code here) creates a menu called ctrlMenu
, which contains the plot options (e.g. Transform, Downsample). This is the "Plot Options" submenu in the image above, and does not appear in the PlotItem.vb.menu.actions()
but can be accessed via PlotItem.ctrlMenu.menuAction()
(StackOverflow reference).
The "Export..." option comes from the underlying GraphicsScene
(source code here) and can be accessed via ViewBox.scene().contextMenu[0]
which gives the "Export..." QAction.
Any of the QAction
can be hidden/shown by QAction.setVisible()
(Qt reference)
Upvotes: 9
Reputation: 25
Found a way to edit & remove the options, check this out: ViewBoxMenu
remove the Export... Options is found here: Export... (contextMenu)
i just cleared the list:
export = self.gui.Display.ui.graphicsView.sceneObj.contextMenu
del export[:]
Upvotes: 1
Reputation: 5546
Use the PlotItem.setMenuEnabled method. Something like:
self.plot = pg.PlotItem()
self.plot.setMenuEnabled(False)
I have not found a way to remove options from it but perhaps it's possible. I would be interested in this as well.
Upvotes: 5