Qcustomplot的简单应用(静态图,动态图)和波形绘制

QTQcustomplot的简单应用,波形绘制

QcustomPlot 库介绍

QCustomPlot是基于Qt的画图和数据可视化的C++控件。相比于Qchart、Qwt、QCustomPlot可实现较好动态刷新特性,同时安装比较简单。

QCustomPlot库的安装与导入

官网中下载QCustomPlot的安装包。将其添加至项目的文件夹中
在这里插入图片描述

在项目中直接添加对应的文件

在这里插入图片描述

初始工作

将主程序的.h文件添加对QCustomPlot.h文件的引用,添加QCustomPlot的引用

#include <QWidget>
#include"qcustomplot.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

同时,需要在.pro的工程文件重添加QT += printsupport,若不添加,则会使QCustomPlot的编译出错

QT       += core gui
QT  +=  printsupport

主界面初始化

添加布局对象和QCustomPlot对象

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    layout=new QVBoxLayout(this);
    customPlot=new QCustomPlot(this);
    customPlot->resize(500,700);
    layout->addWidget(customPlot);
    this->setLayout(layout);
}

其执行的结果,如下图所示,可以明显看到,打印出来坐标轴和网格线

在这里插入图片描述

QCustomPlot 简单应用——1.绘制静态函数曲线

void Widget::drawStatic()
{
    QVector<double>x(101),y(101);
    for(int i=0;i<101;i++)
    {
        x[i]=i/5-10;
        y[i]=x[i]*x[i]*x[i];
    }
    //addGraph 添加图层
    //其数据格式需要为vector类型
    customPlot->addGraph();
    customPlot->graph(0)->setData(x,y);

    customPlot->xAxis->setLabel("x");
    customPlot->yAxis->setLabel("y");

    customPlot->xAxis->setRange(-11,11);
    customPlot->yAxis->setRange(-1100,1100);
}

在这里插入图片描述

QCustomPlot 简单应用——2.绘制较为复杂的静态曲线

void Widget::drawComplexStatic()
{
    customPlot->addGraph();
    //设置线条的颜色和线条的覆盖范围的颜色
    customPlot->graph(0)->setPen(QPen(Qt::blue));
    customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20)));

    customPlot->addGraph();
    customPlot->graph(1)->setPen(QPen(Qt::red));

    //输入数据的样式;
    QVector<double> x(251), y0(251), y1(251);
    for (int i=0; i<251; ++i)
    {
      x[i] = i;
      y0[i] = qExp(-i/150.0)*qCos(i/10.0); // exponentially decaying cosine
      y1[i] = qExp(-i/150.0);              // exponential envelope
    }

    //设置坐标轴的样式
    customPlot->xAxis2->setVisible(true);
    customPlot->xAxis2->setTickLabels(false);
    customPlot->yAxis2->setVisible(true);
    customPlot->yAxis2->setTickLabels(false);
    // 设置上下轴同步
    connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
    connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

    customPlot->graph(0)->setData(x, y0);
    customPlot->graph(1)->setData(x, y1);
    customPlot->graph(0)->rescaleAxes();
    customPlot->graph(1)->rescaleAxes(true);

    //设置对于图形的操作比如放大放小,移动
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

}

在这里插入图片描述

QCustomPlot 简单应用——3.绘制按键与波形联动

#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    layout=new QVBoxLayout(this);
    customPlot=new QCustomPlot(this);
    customPlot->resize(500,700);

    btnClick=new QPushButton;
    btnClick->setText("点击");
//    //drawStatic();
//    drawComplexStatic();
    drawClicked();

    connect(btnClick,&QPushButton::clicked,[=]()
    {
        arrx.append(arrx.count());
        arry.append(qrand()%4096);
        customPlot->graph(0)->setData(arrx,arry);
        customPlot->replot();
        customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

    });
    layout->addWidget(customPlot);
    layout->addWidget(btnClick);

    this->setLayout(layout);
}

void Widget::drawClicked()
{
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::blue));
    //设置画刷,曲线和X轴围成面积的颜色
    customPlot->graph(0)->setBrush(QBrush(QColor(255,255,0)));
    //设置右上角图形标注名称
    customPlot->graph(0)->setName("曲线");
    customPlot->xAxis->setLabel("X");
    customPlot->yAxis->setLabel("Y");
    //设置坐标轴显示范围
    customPlot->xAxis->setRange(0,60);
    customPlot->yAxis->setRange(0,5000);
}

在这里插入图片描述

QCustomPlot 简单应用——4.绘制随时间变化的动态波形

这个例程主要模仿QcustomPlot官网给出的

void Widget::drawDynamic()
{
    customPlot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom
     |QCP::iSelectAxes|QCP::iSelectLegend|QCP::iSelectPlottables);

    //坐标轴
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(QColor(40,110,225)));
    QSharedPointer<QCPAxisTickerTime>timeTicker(new QCPAxisTickerTime);
    timeTicker->setTimeFormat("%h:%m:%s");
    customPlot->xAxis->setTicker(timeTicker);
    customPlot->axisRect()->setupFullAxesBox();
    customPlot->yAxis->setRange(-1.2,1.2);

    //让坐标轴动态的变化
    connect(customPlot->xAxis,SIGNAL(rangeChanged(QCPRange)),
            customPlot->xAxis2,SLOT(setRange(QCPRange)));
    connect(customPlot->yAxis,SIGNAL(rangeChanged(QCPRange)),
            customPlot->yAxis2,SLOT(setRange(QCPRange)));

    QTimer *dataTimer=new QTimer();
    connect(dataTimer,SIGNAL(timeout()),this,SLOT(realTimeDataSlot()));
    dataTimer->start(0);
}

void Widget::realTimeDataSlot()
{
    static QTime time(QTime::currentTime());
    double key=time.elapsed()/1000;
    static double lastPointKey=0;
    if(key-lastPointKey>0.002)
    {
        customPlot->graph(0)->addData(key,qSin(key+qrand()/(double)RAND_MAX*1*qSin(key/0.3843)));
        customPlot->graph(0)->rescaleValueAxis();
        lastPointKey=key;
    }
    customPlot->xAxis->setRange(key,8,Qt::AlignRight);
    customPlot->replot();
}

在这里插入图片描述

  • 14
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QCustomPlot一个用于绘制二维表的开源C++库。它提供了丰富的绘功能,可以绘制静态动态图表。 静态是指在初始化时,绘制一个固定的表。使用QCustomPlot绘制静态表的基本步骤如下: 1. 创建一个QCustomPlot对象,该对象承载了整个表。 2. 调用QCustomPlot对象的成员函数来设置表的一些属性,如标题、坐标轴、刻度等。 3. 使用数据来填充表,可以使用QVector或者std::vector等数据容器来存储数据。 4. 使用QCustomPlot对象的成员函数,如addGraph()或者addPlottable(),将数据添加到表中。 5. 根据需要,可以调整表的样式,如线条颜色、点的形状等。 6. 最后,调用QCustomPlot对象的replot()函数完成表的绘制。 动态绘是指在运行时,根据数据的变化不断更新表。使用QCustomPlot绘制动态图表的基本步骤如下: 1. 创建一个QCustomPlot对象,该对象承载了整个表。 2. 调用QCustomPlot对象的成员函数来设置表的一些属性,如标题、坐标轴、刻度等。 3. 初始化表,可以添加一些初始数据点。 4. 启动一个定时器,在定时器的槽函数中更新数据。 5. 在槽函数中更新表,可以使用QCustomPlot对象的成员函数来更新已有的数据,或者添加新的数据点。 6. 最后,调用QCustomPlot对象的replot()函数刷新表。 需要注意的是,在动态绘时,由于频繁的数据更新,性能方面可能需要做一些优化,以提高绘的效率。 总的来说,QCustomPlot库提供了丰富的功能和灵活的接口,可以满足绘制静态动态图表的需求。掌握了基本的使用方法后,可以根据具体的需求进行更高级的定制和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值