PyQtGraph:平移图时防止QScrollArea滚动

时间:2019-09-04 17:14:15

标签: pyqt pyqt5 pyqtgraph qscrollarea

在PyQtGraph中,您可以使用滚轮放大绘图。但是,在将PyQtGraph嵌入QScrollArea内时,同时滚动这两个控件会放大到悬停的图并滚动QScrollArea。

Example GIF

最小可复制代码:

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

我尝试使用Google搜索该问题,但找到了一种停止平移并允许滚动的方法,但是我希望采用另一种方法:防止滚动并允许平移。

当悬停绘图时,PyQtGraph内部是否可以停止QScrollArea滚动?

1 个答案:

答案 0 :(得分:1)

解决方案是取消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_())