一个实时波形图的封装demo(QT)(qcustomplot)

2024-02-29 08:28

本文主要是介绍一个实时波形图的封装demo(QT)(qcustomplot),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言:

        封装的一个实时波形图的类,可以直接提升使用。 提供了接口,可以更改颜色,样式,等等

参考:

Qt Plotting Widget QCustomPlot - Introduction

另外参考了一个大神的作品,链接没找到。

项目文件:

123盘  

123盘  实时波形图官方版下载丨最新版下载丨绿色版下载丨APP下载-123云盘

CSDN

【免费】一个实时波形图的封装demo(QT)(qcustomplot)资源-CSDN文库

源码

WaveChart文件夹:

1.加入文件qcustomplot.h 和 qcustomplot.cpp:

2.WaveChart.pri文件:

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupportFORMS += \$$PWD/waveWidget.uiHEADERS += \$$PWD/qcustomplot.h \$$PWD/waveWidget.hSOURCES += \$$PWD/qcustomplot.cpp \$$PWD/waveWidget.cpp

3.waveWidget.h文件:

#ifndef WAVEWIDGET_H
#define WAVEWIDGET_H#include <QWidget>
#include <QVBoxLayout>
#include <vector>
#include <cstring>
#include <numeric>
//#include <QScrollBar>
#include "qcustomplot.h"
using std::vector;
namespace Ui {
class WaveWidget;
}class WaveWidget : public QWidget
{Q_OBJECTpublic:explicit WaveWidget(QWidget *parent = nullptr);~WaveWidget();void init(bool isShowTicks=true, bool isShowTickLables=true, bool isShowGrid=true, bool isShowSubGrid=true, bool isShowAxis2=true, bool isGraphBigAndSmall=true);void setTheme(QColor axis, QColor background);void setXAxisLable(const char* name, const char* name2 = "");void setYAxisLable(const char* name, const char* name2 = "");void setPen(Qt::GlobalColor penColor, QCPGraph::LineStyle lineStyle = QCPGraph::lsLine, QColor brushColor = QColor(255,255, 255, 100), bool isShow = true);void addNewData(double data);void setTitle(const char* name);double getMax();double getMin();double getAverage();QCustomPlot*    getTheChartPlot();QCPGraph*       getTheGraph();vector<double>  &getTheVector();private:Ui::WaveWidget *ui;QCustomPlot*    chartPlot = new QCustomPlot(this);      // 图表QCPGraph*       graph;                                  // 曲线//QScrollBar      horizontalScrollBar;vector<double>  graphData;                              // 图表数据,只作记录使用int             timeRange = 10;                         // 时间轴范围QTime time;double          sum = 0.0;};#endif // WAVEWIDGET_H

4.waveWidget.cpp文件

#include "waveWidget.h"
#include "ui_waveWidget.h"WaveWidget::WaveWidget(QWidget *parent) :QWidget(parent),ui(new Ui::WaveWidget)
{ui->setupUi(this);// 初始化图表graph = chartPlot->addGraph();// graph->addData(1,1);// graph->addData(2,3);// 初始化设置// chartPlot->xAxis->setRange(1, 4096);// chartPlot->yAxis->setRange(-1, 350); // 假设灰度值的范围为0-255// chartPlot->xAxis->setLabel("Pixel");// chartPlot->yAxis->setLabel("Gray Value");QVBoxLayout* layout = new QVBoxLayout;layout->addWidget(ui->title);layout->addWidget(chartPlot);chartPlot->setSizePolicy(QSizePolicy::Policy(QSizePolicy::Preferred), QSizePolicy::Policy(QSizePolicy::Expanding));//layout->addWidget((QWidget)horizontalScrollBar);setLayout(layout);// init();}WaveWidget::~WaveWidget()
{delete ui;
}void WaveWidget::init(bool isShowTicks, bool isShowTickLables, bool isShowGrid, bool isShowSubGrid, bool isShowAxis2, bool isGraphBigAndSmall)
{time = QTime::currentTime();// 刻度显示chartPlot->xAxis->setTicks(isShowTicks);chartPlot->yAxis->setTicks(isShowTicks);// 刻度值显示chartPlot->xAxis->setTickLabels(isShowTickLables);chartPlot->yAxis->setTickLabels(isShowTickLables);// 网格显示chartPlot->xAxis->grid()->setVisible(isShowGrid);chartPlot->yAxis->grid()->setVisible(isShowGrid);// 子网格显示chartPlot->xAxis->grid()->setSubGridVisible(isShowSubGrid);chartPlot->yAxis->grid()->setSubGridVisible(isShowSubGrid);// 右和上坐标轴、刻度值显示chartPlot->xAxis2->setVisible(isShowAxis2);chartPlot->yAxis2->setVisible(isShowAxis2);chartPlot->yAxis2->setTicks(isShowAxis2);chartPlot->yAxis2->setTickLabels(isShowAxis2);// make top right axes clones of bottom left axes. Looks prettier:
//    chartPlot->axisRect()->setupFullAxesBox();// make left and bottom axes always transfer their ranges to right and top axes:if(isShowAxis2){connect(chartPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), chartPlot->xAxis2, SLOT(setRange(QCPRange)));connect(chartPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), chartPlot->yAxis2, SLOT(setRange(QCPRange)));}// 暗色主题//setPlotTheme(Qt::white, Qt::black);// 亮色主题setTheme(Qt::black, Qt::white);if(isGraphBigAndSmall){// 可放大缩小和移动chartPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);}// x轴以时间形式显示QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);timeTicker->setTimeFormat("%h:%m:%s");chartPlot->xAxis->setTicker(timeTicker);chartPlot->axisRect()->setupFullAxesBox();chartPlot->replot();
}void WaveWidget::setTheme(QColor axis, QColor background)
{
//----------------------------------------------------------------------------------------//// 坐标标注颜色chartPlot->xAxis->setLabelColor(axis);chartPlot->yAxis->setLabelColor(axis);// 坐标刻度值颜色chartPlot->xAxis->setTickLabelColor(axis);chartPlot->yAxis->setTickLabelColor(axis);// 坐标基线颜色和宽度chartPlot->xAxis->setBasePen(QPen(axis, 1));chartPlot->yAxis->setBasePen(QPen(axis, 1));// 坐标主刻度颜色和宽度chartPlot->xAxis->setTickPen(QPen(axis, 1));chartPlot->yAxis->setTickPen(QPen(axis, 1));// 坐标子刻度颜色和宽度chartPlot->xAxis->setSubTickPen(QPen(axis, 1));chartPlot->yAxis->setSubTickPen(QPen(axis, 1));// 坐标标注颜色chartPlot->xAxis2->setLabelColor(axis);chartPlot->yAxis2->setLabelColor(axis);// 坐标刻度值颜色chartPlot->xAxis2->setTickLabelColor(axis);chartPlot->yAxis2->setTickLabelColor(axis);// 坐标基线颜色和宽度chartPlot->xAxis2->setBasePen(QPen(axis, 1));chartPlot->yAxis2->setBasePen(QPen(axis, 1));// 坐标主刻度颜色和宽度chartPlot->xAxis2->setTickPen(QPen(axis, 1));chartPlot->yAxis2->setTickPen(QPen(axis, 1));// 坐标子刻度颜色和宽度chartPlot->xAxis2->setSubTickPen(QPen(axis, 1));chartPlot->yAxis2->setSubTickPen(QPen(axis, 1));// 整个画布背景色chartPlot->setBackground(background);// 绘图区域背景色chartPlot->axisRect()->setBackground(background);// 刷新绘图chartPlot->replot();
}void WaveWidget::setXAxisLable(const char *name, const char* name2)
{chartPlot->xAxis->setLabel(name);if(strlen(name2)!=0)chartPlot->xAxis2->setLabel(name2);chartPlot->replot();
}void WaveWidget::setYAxisLable(const char *name, const char* name2)
{chartPlot->yAxis->setLabel(name);if(strlen(name2)!=0)chartPlot->yAxis2->setLabel(name2);chartPlot->replot();
}void WaveWidget::setPen(Qt::GlobalColor penColor, QCPGraph::LineStyle lineStyle, QColor brushColor, bool isShow)
{graph->setPen(QPen(penColor)); // 设置线条颜色为蓝色graph->setLineStyle(lineStyle); // 设置线型为直线graph->setVisible(isShow);chartPlot->graph()->setBrush(QBrush(brushColor));chartPlot->replot();
}void WaveWidget::addNewData(double data)
{// 系统当前时间 = 系统运行初始时间 + 系统运行时间static double start = time.hour()*60*60 + time.minute()*60 + time.second() + time.msec()/1000.0;double key = start + time.elapsed()/1000.0;// 设置时间轴chartPlot->xAxis->setRange(key, timeRange, Qt::AlignRight);//chartPlot->rescaleAxes();// 刷新绘图水平滚动条//horizontalScrollBar.setRange(int(start), int(key));  // 刷新滚动条的范围//horizontalScrollBar.setPageStep(1);                  // 设置翻页步长为 1s 的宽度//horizontalScrollBar.setValue(int(key));              // 调整滑块位置到最右边// 更新曲线绘图chartPlot->graph()->addData(key, data);chartPlot->replot(QCustomPlot::rpQueuedReplot);// 存储曲线的当前值graphData.push_back(data);sum += data;
}void WaveWidget::setTitle(const char *name)
{ui->title->setText(name);
}double WaveWidget::getMax()
{return *std::max_element(graphData.begin(),graphData.end());
}double WaveWidget::getMin()
{return *std::min_element(graphData.begin(),graphData.end());
}double WaveWidget::getAverage()
{return 1.0*sum/(graphData.size());
}QCustomPlot *WaveWidget::getTheChartPlot()
{return   chartPlot;
}QCPGraph *WaveWidget::getTheGraph()
{return  graph;
}vector<double> &WaveWidget::getTheVector()
{return graphData;
}

5.测试demo:

新建测试界面:

提升为刚刚的类:

效果:

测试:

    ui->setupUi(this);// 初始化:可以设置参数ui->waveWidget->init();// 可选设置:ui->waveWidget->setXAxisLable("Time");ui->waveWidget->setYAxisLable("Value");ui->waveWidget->setPen(Qt::blue, QCPGraph::lsLine, QColor( 36,141,218,255));ui->waveWidget->setTheme(Qt::red, Qt::white);ui->waveWidget->setTitle("Wave Chart Test");// 测试加入数据:QTimer *timer = new QTimer(this);connect(timer,&QTimer::timeout,[=](){ui->waveWidget->addNewData((qrand()%1000)/1000.0);qDebug()<<"max:"<<ui->waveWidget->getMax()<<"   min:"<<ui->waveWidget->getMin()<<"  average:"<<ui->waveWidget->getAverage();if(num%10==0){qDebug()<<"size: "<<ui->waveWidget->getTheVector().size();ui->waveWidget->getTheVector().clear();qDebug()<<"size: "<<ui->waveWidget->getTheVector().size()<<"********";}if(num>50){timer->stop();}num++;});timer->start(100);

这篇关于一个实时波形图的封装demo(QT)(qcustomplot)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

Go语言利用泛型封装常见的Map操作

《Go语言利用泛型封装常见的Map操作》Go语言在1.18版本中引入了泛型,这是Go语言发展的一个重要里程碑,它极大地增强了语言的表达能力和灵活性,本文将通过泛型实现封装常见的Map操作,感... 目录什么是泛型泛型解决了什么问题Go泛型基于泛型的常见Map操作代码合集总结什么是泛型泛型是一种编程范式,允

基于Qt Qml实现时间轴组件

《基于QtQml实现时间轴组件》时间轴组件是现代用户界面中常见的元素,用于按时间顺序展示事件,本文主要为大家详细介绍了如何使用Qml实现一个简单的时间轴组件,需要的可以参考下... 目录写在前面效果图组件概述实现细节1. 组件结构2. 属性定义3. 数据模型4. 事件项的添加和排序5. 事件项的渲染如何使用

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

python与QT联合的详细步骤记录

《python与QT联合的详细步骤记录》:本文主要介绍python与QT联合的详细步骤,文章还展示了如何在Python中调用QT的.ui文件来实现GUI界面,并介绍了多窗口的应用,文中通过代码介绍... 目录一、文章简介二、安装pyqt5三、GUI页面设计四、python的使用python文件创建pytho

QT实现TCP客户端自动连接

《QT实现TCP客户端自动连接》这篇文章主要为大家详细介绍了QT中一个TCP客户端自动连接的测试模型,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录版本 1:没有取消按钮 测试效果测试代码版本 2:有取消按钮测试效果测试代码版本 1:没有取消按钮 测试效果缺陷:无法手动停

基于Qt实现系统主题感知功能

《基于Qt实现系统主题感知功能》在现代桌面应用程序开发中,系统主题感知是一项重要的功能,它使得应用程序能够根据用户的系统主题设置(如深色模式或浅色模式)自动调整其外观,Qt作为一个跨平台的C++图形用... 目录【正文开始】一、使用效果二、系统主题感知助手类(SystemThemeHelper)三、实现细节

Qt实现文件的压缩和解压缩操作

《Qt实现文件的压缩和解压缩操作》这篇文章主要为大家详细介绍了如何使用Qt库中的QZipReader和QZipWriter实现文件的压缩和解压缩功能,文中的示例代码简洁易懂,需要的可以参考一下... 目录一、实现方式二、具体步骤1、在.pro文件中添加模块gui-private2、通过QObject方式创建

Qt QWidget实现图片旋转动画

《QtQWidget实现图片旋转动画》这篇文章主要为大家详细介绍了如何使用了Qt和QWidget实现图片旋转动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、效果展示二、源码分享本例程通过QGraphicsView实现svg格式图片旋转。.hpjavascript

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设