Search code examples
pyqtpyqt5pyqtgraphqscrollarea

PyQtGraph: prevent QScrollArea scrolling when panning plot


In PyQtGraph you can zoom into the plots with the scroll wheel. However, when embedding PyQtGraph inside a QScrollArea, scrolling both zooms into the hovered plot AND scrolls the QScrollArea.

Example GIF

Minimal reproducable code:

from PyQt5.QtWidgets import QApplication, QScrollArea, QMainWindow
import pyqtgraph as pg

app = QApplication([])
window = QMainWindow()
scroll = QScrollArea(window)
window.setCentralWidget(scroll)

frame = pg.GraphicsLayoutWidget()
plot = frame.addPlot().plot([1,2,3], [2,5,10])

scroll.setWidget(frame)
scroll.setWidgetResizable(True)

frame.setFixedHeight(600)
window.setFixedHeight(500)
window.show()
app.exec_()

I tried googling the issue, and I found a way to stop panning and allow scrolling, however I want it the other way around: prevent scrolling and allow panning.

Is it possible inside PyQtGraph to stop QScrollArea scrolling when a plot is hovered?


Solution

  • The solution is to cancel the wheelEvent of QScrollArea:

    from PyQt5.QtWidgets import QApplication, QScrollArea, QMainWindow
    import pyqtgraph as pg
    
    
    class ScrollArea(QScrollArea):
        def wheelEvent(self, event):
            pass
    
    
    if __name__ == "__main__":
        import sys
    
        app = QApplication(sys.argv)
        window = QMainWindow()
        scroll = ScrollArea(window)
        window.setCentralWidget(scroll)
    
        frame = pg.GraphicsLayoutWidget()
        plot = frame.addPlot().plot([1, 2, 3], [2, 5, 10])
    
        scroll.setWidget(frame)
        scroll.setWidgetResizable(True)
    
        frame.setFixedHeight(600)
        window.setFixedHeight(500)
        window.show()
        sys.exit(app.exec_())