PyQtGraph:在平移图时防止 QScrollArea 滚动

PyQtGraph: prevent QScrollArea scrolling when panning plot

在 PyQtGraph 中,您可以使用滚轮放大图表。但是,当将 PyQtGraph 嵌入到 QScrollArea 中时,滚动会放大悬停的绘图并滚动 QScrollArea。

最少的可重现代码:

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_()

我尝试用谷歌搜索这个问题,我找到了一种停止平移并允许滚动的方法,但是我想要相反的方法:防止滚动并允许平移。

在 PyQtGraph 中是否可以在悬停绘图时停止 QScrollArea 滚动?

解决方法是取消QScrollArea的wheelEvent:

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_())