adding item/action to PlotWidget context menu

190 views
Skip to first unread message

TM 1001

unread,
Apr 6, 2022, 2:32:28 PM4/6/22
to pyqtgraph
Hello,

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 (see picture) instead of above it.  I'd appreciate guidance with fixing this problem.  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_())


 context_menu.png
Reply all
Reply to author
Forward
0 new messages