将项目/操作添加到pyqtgraph plotwidget上下文菜单
我是Python和Pyqt5/pyqtgraph的新手。尝试使用以下代码将操作/项目添加到PlotWidget的上下文菜单。 new_item出现在上下文菜单中的“查看所有”项目的顶部覆盖[有图片,但作为新用户,不允许发布它:-(]而不是上方。我认为我正在添加一个新的上下文菜单(而不是访问默认的上下文菜单并添加到它上)。
from PyQt5 import QtWidgets
import pyqtgraph as pg
class Plot(pg.PlotWidget):
def contextMenuEvent(self, event):
menu = QtWidgets.QMenu(self)
someAction = menu.addAction('New_Item')
res = menu.exec_(event.globalPos())
if res == someAction:
print('Hello')
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
plot = Plot()
plot.show()
sys.exit(app.exec_())][1]
I'm new to python and pyqt5/pyqtgraph. Trying to add an action/item to the context menu of a PlotWidget using the code below. The New_Item appears overlaid on top of the "View All" item in the context menu [have picture, but as a new user, not allowed to post it :-(] instead of above it. I think I'm adding a new context menu (instead of accessing the default context menu and adding to it). I'd appreciate guidance with figuring out what I'm doing wrong. Thanks in advance for the help!
from PyQt5 import QtWidgets
import pyqtgraph as pg
class Plot(pg.PlotWidget):
def contextMenuEvent(self, event):
menu = QtWidgets.QMenu(self)
someAction = menu.addAction('New_Item')
res = menu.exec_(event.globalPos())
if res == someAction:
print('Hello')
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
plot = Plot()
plot.show()
sys.exit(app.exec_())][1]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码救了我的命!我也对如何创建自定义上下文菜单感到困惑。非常感谢!
如果您仍在解决此问题,
请使用
setMenuEnabled(False)
禁用默认上下文菜单。它对您的自定义上下文菜单没有任何作用,一切正常。Your code saved my day! I was also confused about how to create a customized context menu. Many thanks!
If you are still working out this issue,
Use
setMenuEnabled(False)
to disable the default context menu. It just does nothing to your customized context menu and everything works fine.