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

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

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

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

关于PyQtGraph:在平移图时防止 QScrollArea 滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57793068/

相关文章:

python - 使用 itemChange() 限制 QGraphicsItem

python - PyQt4 "Mainwindow.show()"给出类型错误

python - 在Qt中自动调整标签文本的大小-奇怪的行为

time-series - pyqt图: how to plot time series (date and time on the x axis)?

python - 如何将 slider 连接到 PyQt4 中的函数?

python - PyQt5:使用事件进行绘画

python-3.x - PyQt5 QTextEdit 在复制/粘贴时改变颜色

python-3.x - 如何使用 PyQt5 在 Qt 中显示数学排版(MathJax、LaTeX 等)?

python - 绘制实时传感器数据时,PyQtGraph 停止更新并卡住

python - pyqtgraph:同步不同图中轴的缩放