import sys import random from PySide2.QtCore import Qt, QTimer from PySide2.QtGui import QPen from PySide2.QtCharts import QtCharts from PySide2.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QGraphicsView class RealTimeChart(QMainWindow): def __init__(self): super().__init__() # 创建QChart对象并设置标题 self.chart = QtCharts.QChart() self.chart.setTitle("Realtime Chart") # 创建QLineSeries对象并将其添加到QChart中 self.series = QtCharts.QLineSeries() self.chart.addSeries(self.series) # 创建QValueAxis对象并设置范围 self.axis_x = QtCharts.QValueAxis() self.axis_x.setRange(0, 100) self.chart.addAxis(self.axis_x, Qt.AlignBottom) # 创建QValueAxis对象并设置范围 self.axis_y = QtCharts.QValueAxis() self.axis_y.setRange(-1, 1) self.chart.addAxis(self.axis_y, Qt.AlignLeft) # 将QLineSeries与QValueAxis关联 self.series.attachAxis(self.axis_x) self.series.attachAxis(self.axis_y) # 创建QGraphicsScene对象并将QChart添加到场景中 self.view = QGraphicsView() self.scene = QGraphicsScene() self.scene.addItem(self.chart) self.view.setScene(self.scene) # 创建定时器,每隔一段时间更新曲线 self.timer = QTimer(self) self.timer.timeout.connect(self.update_chart) self.timer.start(50) # 设置窗口大小和标题 self.setCentralWidget(self.view) self.setGeometry(100, 100, 800, 600) self.setWindowTitle("Realtime Chart") def update_chart(self): # 随机生成一个数据点并添加到曲线中 x = self.series.count() y = random.uniform(-1, 1) self.series.append(x, y) # 移动坐标轴显示最近的数据 self.axis_x.setRange(x - 100, x) self.view.fitInView(self.chart, Qt.IgnoreAspectRatio) if __name__ == '__main__': app = QApplication(sys.argv) chart = RealTimeChart() chart.show() sys.exit(app.exec_())
时间: 2024-02-14 11:16:13 浏览: 131
bqt:通过Blender中的事件循环帮助引导PySide2的文件
这段代码是一个实时更新曲线的例子,使用了PySide2.QtCharts库。具体实现如下:
1. 导入必要的模块和库:
```
import sys
import random
from PySide2.QtCore import Qt, QTimer
from PySide2.QtGui import QPen
from PySide2.QtCharts import QtCharts
from PySide2.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QGraphicsView
```
2. 创建一个继承自QMainWindow的RealTimeChart类,该类用于显示实时更新的曲线:
```
class RealTimeChart(QMainWindow):
def __init__(self):
super().__init__()
# 创建QChart对象并设置标题
self.chart = QtCharts.QChart()
self.chart.setTitle("Realtime Chart")
# 创建QLineSeries对象并将其添加到QChart中
self.series = QtCharts.QLineSeries()
self.chart.addSeries(self.series)
# 创建QValueAxis对象并设置范围
self.axis_x = QtCharts.QValueAxis()
self.axis_x.setRange(0, 100)
self.chart.addAxis(self.axis_x, Qt.AlignBottom)
# 创建QValueAxis对象并设置范围
self.axis_y = QtCharts.QValueAxis()
self.axis_y.setRange(-1, 1)
self.chart.addAxis(self.axis_y, Qt.AlignLeft)
# 将QLineSeries与QValueAxis关联
self.series.attachAxis(self.axis_x)
self.series.attachAxis(self.axis_y)
# 创建QGraphicsScene对象并将QChart添加到场景中
self.view = QGraphicsView()
self.scene = QGraphicsScene()
self.scene.addItem(self.chart)
self.view.setScene(self.scene)
# 创建定时器,每隔一段时间更新曲线
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_chart)
self.timer.start(50)
# 设置窗口大小和标题
self.setCentralWidget(self.view)
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle("Realtime Chart")
```
3. 实现update_chart()方法,该方法用于更新曲线:
```
def update_chart(self):
# 随机生成一个数据点并添加到曲线中
x = self.series.count()
y = random.uniform(-1, 1)
self.series.append(x, y)
# 移动坐标轴显示最近的数据
self.axis_x.setRange(x - 100, x)
self.view.fitInView(self.chart, Qt.IgnoreAspectRatio)
```
4. 在主程序中创建RealTimeChart实例并显示:
```
if __name__ == '__main__':
app = QApplication(sys.argv)
chart = RealTimeChart()
chart.show()
sys.exit(app.exec_())
```
通过定时器每隔一定时间更新曲线,可以实现实时刷新的效果。
阅读全文