Qt实时曲线

PlotLine.h

//
// Created by dengch on 2023/9/12.
//

#ifndef HIGHALTITUDEPLATFORM_PLOTLINE_H
#define HIGHALTITUDEPLATFORM_PLOTLINE_H
#include "QObject"
#include "QtCharts"
#include <QChartView>
#include <QSplineSeries>
#include <QScatterSeries>
#include <QDebug>
#include <QValueAxis>

class PlotLine: public QObject
{
    Q_OBJECT
public:
    PlotLine(QChartView*  chartView, QObject* parent = nullptr);
    void receivedData(int);

private:
    int maxSize;
    int maxX;
    int maxY;
    QValueAxis* axisY;
    QValueAxis* axisX;
    QList<int> data;
    QSplineSeries* splineSeries;
    QChart* chart;
    QChartView* chartView;

public slots:
    void timerTimeOut();

};


#endif //HIGHALTITUDEPLATFORM_PLOTLINE_H

PlotLine.cpp

//
// Created by dengch on 2023/9/12.
//

#include "PlotLine.h"
#include "QtMath"

PlotLine::PlotLine(QChartView* chartView, QObject* parent) : chartView(chartView), QObject(parent)
{
    maxSize = 51;
    maxX = 5000;
    maxY = 200;
    splineSeries = new QSplineSeries();
    chart = new QChart();
    axisY = new QValueAxis();
    axisX = new QValueAxis();
    chart->legend()->hide();
    chart->setTitle("实时动态曲线示例");
    chart->addSeries(splineSeries);
    axisY->setLabelFormat("%i");
    axisY->setTitleText("温度/℃");
    chart->addAxis(axisY, Qt::AlignLeft);
    axisY->setRange(-maxY, maxY);
    splineSeries->attachAxis(axisY);
    axisX->setLabelFormat("%i");
    axisX->setTitleText("时间/ms");
    chart->addAxis(axisX, Qt::AlignBottom);
    axisX->setRange(0, maxX);
    splineSeries->attachAxis(axisX);
    chartView->setChart(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
    qsrand(time(NULL));
}

void PlotLine::receivedData(int value)
{
    data.append(value);
    while (data.size() > maxSize)
    {
        /* 移除data中第一个数据 */
        data.removeFirst();
    }
    splineSeries->clear();
    int xSpace = maxX / (maxSize - 1);
    for (int i = 0; i < data.size(); ++i)
    {
        splineSeries->append(xSpace * i, data.at(i));
    }
}

void PlotLine::timerTimeOut()
{
    auto data = qrand() % maxY;
    maxY = qMax(maxY, data);
    axisY->setRange(-maxY, maxY * 2);
    receivedData(data);
}

mainwindow.h

//
// Created by dengch on 2023/9/11.
//

#ifndef HIGHALTITUDEPLATFORM_MAINWINDOW_H
#define HIGHALTITUDEPLATFORM_MAINWINDOW_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui
{
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QWidget
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget* parent = nullptr);

    ~MainWindow() override;

private:
    Ui::MainWindow* ui;
    QTimer* timer;
};

#endif //HIGHALTITUDEPLATFORM_MAINWINDOW_H

mainwindow.cpp

//
// Created by dengch on 2023/9/11.
//

// You may need to build the project (run Qt uic code generator) to get "ui_MainWindow.h" resolved

#include "mainwindow.h"
#include "ui_MainWindow.h"
#include "../Chart/Draw.h"
#include "../Chart/PlotLine.h"

MainWindow::MainWindow(QWidget* parent) : QWidget(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QVBoxLayout* vbox = new QVBoxLayout;
    QChartView* chartView = new QChartView(this);
    vbox->addWidget(chartView);
    ui->groupBox_tbq->setLayout(vbox);
    PlotLine* plotLine = new PlotLine(chartView, this);
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, plotLine, &PlotLine::timerTimeOut);
    timer->start(50);
}

MainWindow::~MainWindow()
{
    delete ui;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值