QChart画动态折线图,C++实现,要求如下:每10毫秒产生一个随机数放进一个队列中,这个队列最多能存放1000个数据;每100毫秒刷新一次视图,将队列中的数据画成折线图
时间: 2024-11-25 19:32:57 浏览: 20
在C++中,利用Qt Charts库创建一个动态折线图并满足上述要求,可以参考以下步骤:
1. **包含必要的头文件**:
```cpp
#include <QObject>
#include <QThread>
#include <QQueue>
#include <QDateTime>
#include <QTime>
#include <random>
#include <QChart>
#include <QChartView>
#include <QLineSeries>
#include <QDateTimeAxis>
#include <QValueAxis>
#include <QTimer>
```
2. **创建一个类表示数据和队列**:
```cpp
class DataQueue : public QObject {
Q_OBJECT
public:
explicit DataQueue(QObject *parent = nullptr);
void produceRandomData();
QVector<QPair<qint64, qreal>> getData();
private:
QQueue<QPair<qint64, qreal>> queue;
qint64 lastUpdateTimestamp; // 上次刷新的时间戳
};
```
3. **生产随机数据和更新队列**:
```cpp
DataQueue::DataQueue(QObject *parent) : QObject(parent) {
srand(time(nullptr)); // 初始化随机种子
}
void DataQueue::produceRandomData() {
if (!queue.full()) { // 队列未满
qint64 timestamp = QDateTime::currentMSecsSinceEpoch(); // 当前时间戳
qreal randomValue = static_cast<qreal>(rand()) / RAND_MAX; // 随机浮点数
queue.enqueue({timestamp, randomValue});
lastUpdateTimestamp = timestamp;
}
}
```
4. **更新视图函数**:
```cpp
void updateChart(QChartView *chartView, const DataQueue &dataQueue) {
if (QDateTime::currentMSecsSinceEpoch() - dataQueue.lastUpdateTimestamp > 100000) {
chartView->chart()->removeAllSeries();
chartView->chart()->setBackgroundBrush(Qt::transparent); // 清空并透明化背景
}
while (!dataQueue.queue.isEmpty() && dataQueue.queue.size() > 1000) {
dataQueue.queue.pop(); // 删除最旧的数据
}
QLineSeries* lineSeries = dynamic_cast<QLineSeries*>(chartView->chart()->seriesAt(0));
if (!lineSeries) {
lineSeries = new QLineSeries();
chartView->chart()->addSeries(lineSeries);
}
for (const auto& pair : dataQueue.queue) {
lineSeries->append(pair.first, pair.second);
}
// 更新坐标轴
QDateAxis *axisX = qobject_cast<QDateAxis*>(chartView->chart()->axes()[0]);
axisX->setTickCount(0); // 不显示刻度
QValueAxis *axisY = qobject_cast<QValueAxis*>(chartView->chart()->axes()[1]);
axisY->setLabelFormat("%.2f"); // 设置Y轴标签格式
chartView->update();
}
```
5. **主循环和定时器**:
```cpp
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
DataQueue dataProducer;
QThread producerThread(&dataProducer);
producerThread.start();
QChartView *chartView = new QChartView(createChart(), this);
connect(&dataProducer, &DataQueue::produceRandomData, &producerThread, &QThread::exec); // 异步生产数据
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, [chartView](QChartView *chartView) {
updateChart(chartView, dataProducer);
});
timer->start(10000); // 每100毫秒刷新
return app.exec();
}
```
6. **完整绘图函数**:
```cpp
QChart *createChart() {
QChart *chart = new QChart();
chart->setAnimationOptions(QChart::NoAnimation);
chart->setBackgroundBrush(Qt::transparent);
QDateAxis *axisX = new QDateTimeAxis();
axisX->setTickPosition(QDateTimeAxis::Bottom);
chart->addAxis(axisX, Qt::AlignBottom);
axisX->setVisible(false);
QValueAxis *axisY = new QValueAxis();
axisY->setLabelFormat("%.2f");
chart->addAxis(axisY, Qt::AlignLeft);
chartView->chart()->setBackgroundBrush(Qt::transparent);
QLineSeries *lineSeries = new QLineSeries();
chart->addSeries(lineSeries);
return chart;
}
```
阅读全文