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

2024-03-04 06:18

本文主要是介绍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_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic: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 cosiney1[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();
}

在这里插入图片描述

这篇关于Qcustomplot的简单应用(静态图,动态图)和波形绘制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/772241

相关文章

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

Python多线程应用中的卡死问题优化方案指南

《Python多线程应用中的卡死问题优化方案指南》在利用Python语言开发某查询软件时,遇到了点击搜索按钮后软件卡死的问题,本文将简单分析一下出现的原因以及对应的优化方案,希望对大家有所帮助... 目录问题描述优化方案1. 网络请求优化2. 多线程架构优化3. 全局异常处理4. 配置管理优化优化效果1.

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知