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

本文介绍如何使用QCustomPlot库在Qt应用中绘制实时更新的波形图表。包括库的引入、工程配置及绘图代码实现,并展示了最终效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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(){
    //可变数组存放绘图坐标数据
    QVector<double>x(100),y(100);
    QVector<double>x1(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.效果图:

    

    

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值