Important: Please read the Qt Code of Conduct -

Is it possible to capture mouse in QGraphicsView?

  • Hi,

    I've created a UI file with the Editor, it does contain a QGraphicsView (promoted to QChart) and I would like to know if it's possible to capture the mouse click events on the QChart (or QGraphicsView) and handle them in order to show a context menu when right-click on the chart.

    From a slight research it seems to me I need to Subclass the QGraphicsView and implement an eventFilter, is that right?

    I'm using PySide2 and fbs (to generate the application installer). Here's how I load the UI into PySide2:

    class UiLoader(QUiLoader):
        Override the createWidget method so we can load a promoted QGraphicsView
        otherwise the QUiLoader is not able to load it
        def createWidget(self, classname, parent=None, name=""):
            if classname == "QChartView":
                return QtCharts.QChartView(parent)
            return super().createWidget(classname, parent, name)
    class AppContext(ApplicationContext):
        def __init__(self, *args, **kwargs):
            super(AppContext, self).__init__()
            # Load UI file
            ui_file = self.get_resource("mainwindow.ui")
            self.file = QFile(ui_file)
            self.loader = UiLoader()
            self.window = self.loader.load(self.file)
        def run(self):
            # Creating QChart
            #  # noqa: E501
            self.chart = QtCharts.QChart()
            # Chart steps setup, etc...
            # Creating QChartView
    if __name__ == '__main__':
        appctxt = AppContext()
        exit_code =

  • @CarlosDiaz

    in order to show a context menu when right-click on the chart

    I don't know anything about QChart. But for QGraphicsView there is QGraphicsView::contextMenuEvent(). See the PyQt example at ?

Log in to reply