Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Is it possible to capture mouse in QGraphicsView?
Forum Updated to NodeBB v4.3 + New Features

Is it possible to capture mouse in QGraphicsView?

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 434 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CarlosDiaz
    wrote on last edited by CarlosDiaz
    #1

    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.file.open(QFile.ReadOnly)
    
            self.loader = UiLoader()
            self.window = self.loader.load(self.file)
    
        def run(self):
            self.app.setStyle('Fusion')
    
            # Creating QChart
            # https://doc.qt.io/qtforpython-5/tutorials/datavisualize/add_chart.html  # noqa: E501
            self.chart = QtCharts.QChart()
            self.chart.setAnimationOptions(QtCharts.QChart.AllAnimations)
    
            # Chart steps setup, etc...
    
            # Creating QChartView
            self.window.graphicsView.setChart(self.chart)
            self.window.graphicsView.setRenderHint(QPainter.Antialiasing)
    
    if __name__ == '__main__':
        appctxt = AppContext()
        exit_code = appctxt.run()
        sys.exit(exit_code)
    
    JonBJ 1 Reply Last reply
    0
    • C CarlosDiaz

      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.file.open(QFile.ReadOnly)
      
              self.loader = UiLoader()
              self.window = self.loader.load(self.file)
      
          def run(self):
              self.app.setStyle('Fusion')
      
              # Creating QChart
              # https://doc.qt.io/qtforpython-5/tutorials/datavisualize/add_chart.html  # noqa: E501
              self.chart = QtCharts.QChart()
              self.chart.setAnimationOptions(QtCharts.QChart.AllAnimations)
      
              # Chart steps setup, etc...
      
              # Creating QChartView
              self.window.graphicsView.setChart(self.chart)
              self.window.graphicsView.setRenderHint(QPainter.Antialiasing)
      
      if __name__ == '__main__':
          appctxt = AppContext()
          exit_code = appctxt.run()
          sys.exit(exit_code)
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 https://stackoverflow.com/a/14972629/489865 ?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved