将项目/操作添加到pyqtgraph plotwidget上下文菜单

发布于 2025-01-20 03:18:45 字数 709 浏览 3 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2025-01-27 03:18:45

你的代码救了我的命!我也对如何创建自定义上下文菜单感到困惑。非常感谢!

如果您仍在解决此问题,

class Plot(pg.PlotWidget):

    def __init__(self):
        super().__init__()
        self.setMenuEnabled(False)

    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()
    app.exec_()

请使用 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,

class Plot(pg.PlotWidget):

    def __init__(self):
        super().__init__()
        self.setMenuEnabled(False)

    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()
    app.exec_()

Use setMenuEnabled(False) to disable the default context menu. It just does nothing to your customized context menu and everything works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文