[Python] PyQtGraph ROI Mouse Events Not Working Inside QGraphicsProxyWidget

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    When I place a PyQtGraph PlotWidget with ROIs inside a QGraphicsProxyWidget, the ROIs don't respond to some of the mouse interactions. Except the hover events, no operations work with the ROI e.g. drag, click, etc.

    However, the same ROI works perfectly when:

    • The PlotWidget is used as a standalone widget
    • The PlotWidget is placed inside a QMdiSubWindow

    Code Example

    Here's a minimal reproducible example:

    from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget, QGraphicsScene, QGraphicsView
    from PySide6.QtCore import Qt
    import pyqtgraph as pg

    class Plot(QWidget):
    def __init__(self):
    super().__init__()
    # Create PlotWidget
    self.plot = pg.PlotWidget()
    self.plot.showGrid(True, True)
    self.plot.setBackground('w')

    # Add plot to layout
    layout = QVBoxLayout(self)
    layout.addWidget(self.plot)

    # Add a rectangle ROI
    roi = pg.RectROI([0, 0], [1, 1], pen='r')
    roi.setAcceptedMouseButtons(Qt.LeftButton)
    roi.sigClicked.connect(lambda: print("Clicked"))
    roi.sigHoverEvent.connect(lambda: print("Hovering"))
    roi.sigRegionChangeStarted.connect(lambda: print("Region change started"))
    roi.sigRegionChangeFinished.connect(lambda: print("Region change ended"))
    self.plot.addItem(roi)

    # Failing case: ROI doesn't respond to mouse events
    if __name__ == '__main__':
    app = QApplication([])
    plot = Plot()
    view = QGraphicsView()
    scene = QGraphicsScene()
    proxy = scene.addWidget(plot)
    view.setScene(scene)
    view.show()
    app.exec()


    Working Examples

    # Case 1: ROI works in standalone widget
    if __name__ == '__main__':
    app = QApplication([])
    plot = Plot()
    plot.show()
    app.exec()


    # Case 2: ROI works in QMdiSubWindow
    if __name__ == '__main__':
    app = QApplication([])
    window = QMainWindow()
    mdi = pg.QtWidgets.QMdiArea()
    window.setCentralWidget(mdi)
    sub = pg.QtWidgets.QMdiSubWindow()
    plot = Plot()
    sub.setWidget(plot)
    mdi.addSubWindow(sub)
    window.show()
    app.exec()


    Expected Behavior

    The ROI should respond to mouse events such as clicking, and dragging, just as it does when the PlotWidget is used directly or in a QMdiSubWindow.

    Actual Behavior

    When using QGraphicsProxyWidget, the ROI doesn't respond to the clicking and dragging interactions, and the signals (sigClicked, sigRegionChangeStarted, etc.) are not triggered.

    What I've Tried

    I've attempted to:

    • Set different mouse event flags for the proxy widget
    • Explicitly set event filter on the proxy widget
    • Check if setHandlesChildEvents affects behavior
    • Compare different Qt widget containers (QMdiSubWindow works, QGraphicsProxyWidget doesn't)

    Is there a way to make ROIs respond to mouse events when wrapped in a QGraphicsProxyWidget? Any insights or solutions would be greatly appreciated!

    Continue reading...

Compartilhe esta Página