Qt 使用QCustomPlot类 实现实时波形绘制

使用QCustomPlot类实现波形绘制 

1.首先需要将QCustomPlot类文件(.c/.h)文件添加到工程文件中 

   QCustomPlot 官方下载地址: 

    http://www.qcustomplot.com/index.php/download

    将下载的文件夹中的  .c/.h 文件添加到工程文件中    

2.然后配置工程文件,即在 .pro 文件中添加 :

     QT += widgets printsupport

3.工程源文件下载地址 :

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
   QPushButton *Button = new QPushButton(this);
   Button->setText("绘图");
   connect(Button,&QPushButton::pressed,this,&Widget::PlotDeal);

   QPushButton *Button1 = new QPushButton(this);
   Button1->setText("关闭");
   Button1->move(100,0);
   connect(Button1,&QPushButton::pressed,this,&Widget::PlotClose);

   timer = new QTimer;             //定时器
   connect(timer, SIGNAL(timeout()), this, SLOT(PlotReplot()));//定时器槽函数
   timer->start(500);                //定时器定时时间

   cPlot.addGraph();
   cPlot.graph(0)->setName("Data1");
   cPlot.graph(0)->setPen(QPen(Qt::blue));

   cPlot.addGraph();
   cPlot.graph(1)->setName("Data1");
   cPlot.graph(1)->setPen(QPen(Qt::red));

   cPlot.setGeometry(100,100,1000,500);

   cPlot.xAxis->setLabel("x");
   cPlot.yAxis->setLabel("y");
   cPlot.xAxis->setRange(0,100);
   cPlot.yAxis->setRange(0,100);
}

Widget::~Widget()
{

}

void Widget::PlotDeal(){   
    cPlot.show();
}

void Widget::PlotClose(){
    cPlot.close();
}


void Widget::PlotReplot(){
    //可变数组存放绘图坐标数据
    QVectorx(100),y(100);
    QVectorx1(100),y1(100);

    //填充数据
    for(int i=0;i<100;i++){
        x[i] = i;
        y[i] = rand() % 40 + 40;

        x1[i] = i;
        y1[i] = rand() % 40;
    }
    //设置数据(显示)
    cPlot.graph(0)->setData(x,y);
    cPlot.graph(1)->setData(x1,y1);
    //刷新
    cPlot.replot();
}

  

4.效果图:

    Qt 使用QCustomPlot类 实现实时波形绘制_第1张图片

    

你可能感兴趣的:(Qt,C++)