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

1 篇文章 0 订阅
1 篇文章 0 订阅

使用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.效果图:

    

    

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
QCustomPlot 是一款功能强大的 Qt 绘图库,它支持多种图表型,并且具有丰富的交互功能和自定义能力。 要在 QCustomPlot实现实时波形图,可以按照以下步骤进行: 1. 创建一个 QCustomPlot 对象,并添加一个 QCPGraph 对象作为波形图的曲线。 ```cpp QCustomPlot *customPlot = new QCustomPlot(this); QCPGraph *graph = customPlot->addGraph(); ``` 2. 设置波形图的样式,包括线条颜色、宽度、样式等。 ```cpp graph->setPen(QPen(Qt::blue)); graph->setLineStyle(QCPGraph::lsLine); graph->setAntialiased(true); ``` 3. 在每次需要更新波形图时,向 QCPGraph 中添加新的数据点,并重新绘制图形。 ```cpp double time = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0; // 获取当前时间戳 double value = ...; // 获取需要绘制的数据值 graph->addData(time, value); // 添加新的数据点 customPlot->rescaleAxes(); // 自适应缩放坐标轴 customPlot->replot(); // 重新绘制图形 ``` 4. 在实时绘制过程中,为了不影响绘图性能,可以设置 QCustomPlot 对象的自动重绘模式为“不自动重绘”。 ```cpp customPlot->setNoAntialiasingOnDrag(true); customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables); customPlot->setAntialiasedElements(QCP::aeNone); customPlot->setNotAntialiasedElements(QCP::aeAll); customPlot->setAutoAddPlottableToLegend(false); customPlot->setAutoMargin(true); customPlot->setAutoSubTicks(true); customPlot->setAutoTickStep(true); customPlot->setMultiSelectModifier(Qt::ControlModifier); customPlot->setNoAntialiasingOnDrag(true); customPlot->setOpenGl(true); customPlot->setPlottingHint(QCP::phFastPolylines, true); customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables); ``` 以上是使用 QCustomPlot 绘制实时波形图的基本步骤,实际应用中,还可以根据需要进行细节调整和性能优化。
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值