当前位置: 首页 > news >正文

QCustomPlot横坐标为毫秒级的时间轴数据展示的实时刷新数据功能

在这里插入图片描述
头文件

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTimer>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();int realtimeDataSlot();
private:Ui::Widget *ui;QTimer* dataTimer;
};
#endif // WIDGET_H

源文件

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);dataTimer = new QTimer(this);ui->customPlot->addGraph();ui->customPlot->graph(0)->setPen(QPen(QColor(40, 110, 255)));//设置x轴格式QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);//设置tick个数timeTicker->setTickCount(10);//毫秒为单位timeTicker->setTimeFormat("%h:%m:%s.%z");ui->customPlot->xAxis->setTicker(timeTicker);ui->customPlot->axisRect()->setupFullAxesBox();//设置Y轴的范围ui->customPlot->yAxis->setRange(-1, 1);//设置x轴为当前时间int nowtime = QTime::currentTime().msecsSinceStartOfDay(); //获取精度为毫秒double key = nowtime;//设置初始坐标轴的x坐标ui->customPlot->xAxis->setRange(key * 0.001, 10, Qt::AlignRight);connect(dataTimer, &QTimer::timeout, this, &Widget::realtimeDataSlot);dataTimer->start(0);
}Widget::~Widget()
{delete ui;
}int Widget::realtimeDataSlot()
{//key, 8, Qt::AlignRightint nowtime = QTime::currentTime().msecsSinceStartOfDay();double key = nowtime;static double lastPointKey;//记录每次刷新数据的时候添加点的个数,此处只想知道时钟滴答准确性,没什么作用static int dataCount;if (key - lastPointKey >= 2) //两毫秒保存一个数据{//产生随机数,注意此处只是把x轴转为秒为单位展示,实际上还是毫秒级别ui->customPlot->graph(0)->addData(key*0.001, QRandomGenerator::global()->bounded(110));lastPointKey = key;dataCount++;}static double lastMoveKey;//下面的逻辑要基于自己的想法实现,主要是多少秒刷一次x轴和移动坐标轴的问题,我的想法是一秒移动一次if(key - lastMoveKey >= 1000){ui->customPlot->xAxis->setRange(key*0.001, 10, Qt::AlignRight);  //注意此处设置key点在最右侧//此处需要删除10秒前的数据ui->customPlot->graph(0)->data()->removeBefore(key*0.001-10);lastMoveKey = key;qDebug() << dataCount;dataCount=0;}//y轴自适应ui->customPlot->graph(0)->rescaleValueAxis();ui->customPlot->replot();return 0;
}
http://www.lryc.cn/news/132913.html

相关文章:

  • STM32/AT32 MCO管脚输出时钟配置
  • “SRP模型+”多技术融合在生态环境脆弱性评价模型构建、时空格局演变分析与RSEI 指数的生态质量评价
  • 【大虾送书第六期】搞懂大模型的智能基因,RLHF系统设计关键问答
  • 超越函数界限:探索JavaScript函数的无限可能
  • PHP实现轻量级WEB服务器接收HTTP提交的RFID刷卡信息并回应驱动读卡器显示播报语音
  • Neo4j之with基础
  • 60页数字政府智慧政务大数据资源平台项目可研方案PPT
  • 循环神经网络RNN完全解析:从基础理论到PyTorch实战
  • 【SA8295P 源码分析】52 - 答疑之 QNX 创建镜像、Android修改CMDLINE
  • 网络安全法律
  • Redis缓存问题(穿透, 击穿, 雪崩, 污染, 一致性)
  • 网络时代拟态环境的复杂化
  • 湘潭大学 湘大 XTU OJ 1055 整数分类 题解(非常详细)
  • 什么是视频的编码和解码
  • LeetCode 2681. Power of Heroes【排序,数学,贡献法】2060
  • AVL树的讲解
  • Unity 之 Input类
  • 亚信科技AntDB数据库连年入选《中国DBMS市场指南》代表厂商
  • AMBA总线协议(3)——AHB(一)
  • Git commit与pull的先后顺序
  • HarmonyOS/OpenHarmony应用开发-ArkTS语言渲染控制ForEach循环渲染
  • Powered by Paraverse | 平行云助力彼真科技打造演出“新物种”
  • 企微配置回调服务
  • 机器人远程控制软件设计
  • 面试题-React(二):React中的虚拟DOM是什么?
  • 分布式链路追踪——Dapper, a Large-Scale Distributed Systems Tracing Infrastructure
  • 【IEEE会议】第二届IEEE云计算、大数据应用与软件工程国际学术会议 (CBASE2023)
  • Streamlit项目:基于讯飞星火认知大模型开发Web智能对话应用
  • [Vue]解决npm run dev报错node:internal/modules/cjs/loader:1031 throw err;
  • nginx反向代理后实现nginx和apache两种web服务器能够记录客户端的真实IP地址