记录始于20200710-Qt绘制动态平滑曲线

程序媛一枚,目前在读研究生。本专业生物医学工程,自学转行互联网偏后端。博客用来做输出,如果能给到更多人入门级的帮助,那自然是极好的!java比较常用,偶尔会记录些跟课题组相关的工作会用不太熟练的C++。

日常在课题组打黑工,最近为了配合项目组工作学了C++。给了个小任务练手:基于Qt5实现一个能实时绘制的动态平滑曲线。暂时没法连采集数据的设备,这里就人为设定给随机数的方式。采用QtCharts组件实现,还算比较简单,代码量不多,仅涉及三个文件:两个源文件,一个头文件。代码我都贴在下面,注释也都在代码里。欢迎在博客下留言交流~
记录始于20200710-Qt绘制动态平滑曲线_第1张图片

.h文件:mainwindow.h

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include 
//#include 
#include 
#include 
#include 
#include 
#include     //画笔,#include 
#include //画布
#include 
#include 
#include 

using namespace QtCharts;

//QT_CHARTS_USE_NAMESPACE   //使用qtchart需要加入这条语句

class MainWidget : public QWidget
{
    Q_OBJECT

public:
    explicit MainWidget(QWidget *parent = 0);
    ~MainWidget();

protected:
    //定时器触发事件,重构
    void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;

private:
    void dataReceived(int value);
    int timerId;
    int maxSize;  // data 最多存储 maxSize 个元素
    int maxX;
    int maxY;
    QList data; // 存储业务数据的 list

    QChart *chart;
    QChartView *chartView;
    QSplineSeries *splineSeries;//用来创建曲线
    QScatterSeries *scatterSeries;//空心折线图
};

#endif // MAINWIDGET_H

mainwindow.cpp

#include "mainwidget.h"

#include 
#include 

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent)
{
    maxSize = 31; // 只存储最新的 31 个数据
    maxX = 300;
    maxY = 100;

    splineSeries = new QSplineSeries();
    scatterSeries = new QScatterSeries();
    scatterSeries->setMarkerSize(8);

    chart = new QChart();
    chart->addSeries(splineSeries);
    chart->addSeries(scatterSeries);
    chart->legend()->hide();
    chart->setTitle("Real-Time Curve");
    chart->createDefaultAxes();
    chart->axisX()->setRange(0, 300);
    chart->axisY()->setRange(0, maxY);

    chartView = new QChartView(chart);

    QValueAxis *axisx = new QValueAxis;
    axisx->setGridLineVisible(false);
    axisx->setTitleText("sample time(s)");
    chartView->chart()->setAxisX(axisx, splineSeries);

    QValueAxis *axisy = new QValueAxis;
    axisy->setGridLineVisible(false);
    axisy->setTitleText("height");
    chartView->chart()->setAxisY(axisy,splineSeries);

    chartView->setRenderHint(QPainter::Antialiasing);

    QHBoxLayout *layout = new QHBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(chartView);
    setLayout(layout);

    timerId = startTimer(200);
    qsrand(QDateTime::currentDateTime().toTime_t());
}

MainWidget::~MainWidget()
{
}

void MainWidget::timerEvent(QTimerEvent *event) {
     // 产生一个数据,模拟不停的接收到新数据
     if (event->timerId() == timerId) {
         int newData = qrand() % (maxY + 1);
         dataReceived(newData);
     }
 }
void MainWidget::dataReceived(int value) {
     data << value;
     // 数据个数超过了最大数量,则删除所有数据,从头开始。
     while (data.size() > maxSize) {
         data.removeFirst();
     }
     // 界面被隐藏后就没有必要绘制数据的曲线了
     if (isVisible()) {
         splineSeries->clear();
         scatterSeries->clear();
         int dx = maxX / (maxSize-1);
         for (int i = 0; i < data.size(); ++i) {
             splineSeries->append(i*dx, data.at(i));
            scatterSeries->append(i*dx, data.at(i));
         }
     }
 }

main.cpp

#include "mainwidget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWidget w;
    w.show();

    return a.exec();
}

你可能感兴趣的:(记录始于20200710-Qt绘制动态平滑曲线)