Qt总结(六)-- 图表数据曲线
不要浮躁,多总结
QCustomplot
源代码下载
◆ 运行两个example,优秀
.pro文件添加QT += printsupport
解读axis-tags
坐标轴
QCustomplot 类中的坐标轴成员有QCPAxis *xAxis, *yAxis, *xAxis2, *yAxis2;
其中*xAxis
表示下横坐标轴,*xAxis2
表示上横坐标轴,*yAxis
左纵坐标轴,*yAxis2
右纵坐标轴
但可以有每个区域可以添加多个坐标轴,如使用mPlot->axisRect()->addAxis(QCPAxis::atLeft);
添加一个左标轴。
使用mPlot->axisRect()->axis(QCPAxis::atLeft, index)
来访问,index是编号。
xAxis、xAxis2、yAxis、yAxis2这四个默认指向index为0的坐标轴。
◆setRange()有两种重载形式,如直接setRange(1,3) ,就是设置1~3
另一种重载形式如下,setRange(1,3,Qt::AlignRight) 是设置-2~1
void QCPAxis::setRange(double position, double size, Qt::AlignmentFlag alignment)
{if (alignment == Qt::AlignLeft)setRange(position, position+size);else if (alignment == Qt::AlignRight)setRange(position-size, position);else // alignment == Qt::AlignCentersetRange(position-size/2.0, position+size/2.0);
}
曲线图 QPointer<QCPGraph>
创建并设置对应的横纵坐标轴
QPointer<QCPGraph> mGraph;
mGraph = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, 0));
mGraph->setPen(QPen(QColor(250, 120, 0)));
使用AxisTag
可以在坐标轴上实时显示当前值
mTag = new AxisTag(mGraph->valueAxis()); //创建并指定坐标轴
mTag->setPen(mGraph->pen());
添加数据使用addData,两种重载形式
void QCPGraph::addData(const QVector<double> &keys, const QVector<double> &values, bool alreadySorted)
void QCPGraph::addData(double key, double value)
◆ onlyEnlarger true只放大不缩小 inKeyRange true则根据当前显示的数值缩放,有一种实时跟踪的效果
mGraph->rescaleValueAxis(bool onlyEnlarger , bool inKeyRange ); //ValueAxis纵坐标轴 KeyAxis横坐标轴
◆设置完graph的数值后
mPlot->replot(); //更新
解读scrollbar-axis-range-control
使用枚举enum Interaction
可以设置各种选项,实现缩放
//QCP::iRangeDrag 可以拖动坐标轴 QCP::iRangeZoom 鼠标滚轮放大缩小//QCP::iSelectPlottables 选中曲线加粗 QCP::iMultiSelect 可以多选//QCP::iSelectAxes 坐标轴可选 QCP::iSelectLegend 图例可选ui->plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); //交互
连接坐标轴和滑动条,同步改变
// create connection between axes and scroll bars: connect(ui->horizontalScrollBar, SIGNAL(valueChanged(int)), this, SLOT(horzScrollBarChanged(int)));connect(ui->verticalScrollBar, SIGNAL(valueChanged(int)), this, SLOT(vertScrollBarChanged(int)));connect(ui->plot->xAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(xAxisChanged(QCPRange)));connect(ui->plot->yAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(yAxisChanged(QCPRange)));
MyQCP
结合axis-tags和scrollbar-axis-range-control两者功能
功能
(1)动态显示数据曲线 (axis-tags)
(2)双击控件弹出一个大窗口,显示历史500帧的数据,可以拖动放大,缩小 (scrollbar-axis-range-control)
connect(mPlot,SIGNAL(mouseDoubleClick(QMouseEvent*)),this,SLOT(mouseDoubleClickSlot(QMouseEvent*)));
使用
(1)工程加入axistag.cpp、qcustomplot.cpp、myqcp.cpp及对应的头文件,由于添加时是添加整个文件夹(便于管理),所以包含头文件时得指明其绝对路径。。pro 文件增加Qt +=printsupport
(2) MyQCP* plot
plot = new MyQCP(this); //this 父控件
plot->setSize(330,180);
plot->setGeometry(420,70,330,180);
(3)增加数据时调用addGraphData(),其中key 横坐标使用 plot->mGraph->dataCount(),
void MyQCP::addGraphData(double key, double value)
补充:
(1)点击选择曲线上的点
// connect slot that shows a message in the status bar when a graph is clicked:connect(plot, SIGNAL(plottableClick(QCPAbstractPlottable*,int,QMouseEvent*)), this, SLOT(graphClicked(QCPAbstractPlottable*,int)));
槽函数void graphClicked(QCPAbstractPlottable *plottable, int dataIndex)
里获取选取的点的值
double dataValue = plottable->interface1D()->dataMainValue(dataIndex);
新建一个graph,设置点型和大小,然后只绘制被选中的点
//graph2 用于点被选中时加粗显示
plot->addGraph();
plot->graph(2)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDiamond));
QPen graphPen;
graphPen.setColor(QColor(100,0, 255));
graphPen.setWidthF(5);
plot->graph(2)->setPen(graphPen);
完整的槽函数如下
void ScrollBox::graphClicked(QCPAbstractPlottable *plottable, int dataIndex)
{//点击曲线拐点附近的非曲线区域有时也会触发点击事件,dataIndex为0// since we know we only have QCPGraphs in the plot, we can immediately access interface1D()// usually it's better to first check whether interface1D() returns non-zero, and only then use it.double dataValue = plottable->interface1D()->dataMainValue(dataIndex);QVector<double> d0,d1;plot->graph(2)->setData(d0,d1); //空的数据,清空graphplot->graph(2)->addData((qreal)dataIndex, dataValue);plot->replot();
}
(2)右键菜单选择要显示的曲线
- 给控件设置上下文菜单策略
setContextMenuPolicy(Qt::CustomContextMenu)
- 设置该策略后当我们右键点击控件时qt会发送一个信号
void QWidget::customContextMenuRequested(const QPoint &pos)
,其中参数pos
用来传递右键点击时的鼠标的坐标,这个坐标一般是相对于控件左上角而言的; - 设置相应的槽函数,在槽函数中显示菜单,菜单设置好
QAction
控制曲线的显示或隐藏
[具体步骤]
(1)头文件添加
#include<QAction>
#include<QMenu>//右键菜单QAction * graph0_Action;QAction * graph1_Action;QMenu * graphMenu;enum graphPlot {showGraph0=0x001,showGraph1=0x010,showGraph2=0x100,hideGraph0=0x110,hideGraph1=0x101,hideGraph2=0x011,};
(2)cpp中
mPlot->setContextMenuPolicy(Qt::CustomContextMenu);
connect(mPlot,&QWidget::customContextMenuRequested,[=](const QPoint &pos)
{qDebug()<<pos;//参数pos用来传递右键点击时的鼠标的坐标,这个坐标一般是相对于控件左上角而言的graphMenu->exec(QCursor::pos());
});
graphMenu初始化
//右键菜单选择显示曲线
graph0_Action = new QAction(QString::fromLocal8Bit("原始数据"),this);
graph1_Action = new QAction(QString::fromLocal8Bit("卡尔曼滤波"),this);
graph0_Action->setCheckable(true); //可勾选
graph1_Action->setCheckable(true);graphPlotFlag = 0;
graphPlotFlag|=graphPlot::showGraph1; //默认显示
graph1_Action->setChecked(true);
checkState0 =false; checkState1 = true;graphMenu = new QMenu(this);
//动作添加到菜单
graphMenu->addAction(graph0_Action);
graphMenu->addAction(graph1_Action);//给动作设置信号槽
connect(graph0_Action, &QAction::triggered, [=]()
{if(!checkState0){graphPlotFlag|=graphPlot::showGraph0;graph0_Action->setChecked(true);checkState0 = true;}else{graphPlotFlag&=graphPlot::hideGraph0;graph0_Action->setChecked(false);checkState0 = false;}});
//给动作设置信号槽
connect(graph1_Action, &QAction::triggered, [=]()
{if(!checkState1){graphPlotFlag|=graphPlot::showGraph1;graph1_Action->setChecked(true);checkState1 = true;}else{graphPlotFlag&=graphPlot::hideGraph1;graph1_Action->setChecked(false);checkState1 = false;}
});
各版本下载列表,5.8以后没有单独的msvc版本,安装时可以勾选
Qt Charts 在Qt4就有了,不过需要商业许可。qt5.7.0开始直接开放了,安装时默认会勾选,如左图所示。在工程的.pro文件中加入QT += charts
就可以使用了
设置曲线样式
//点与点直接的连接类型 lsNone 无连接,散点图 lsLine 直线连接
mPlot->graph(1)->setLineStyle(QCPGraph::lsNone);
mPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDiamond));//点型
QPen graphPen;
graphPen.setColor(QColor(100,0, 255));//颜色
graphPen.setWidthF(5);//粗细
mPlot->graph(1)->setPen(graphPen);
简单显示一条曲线
(1)头文件加入
#include <QValueAxis>
#include<QChartView>
#include<QLineSeries>
#include<QPieSeries>
using namespace QtCharts;
(2)
在ui设计界面使用Qt Creator添加一个widget组件,然后提升为QChartView。提升的时候,这样写提升为的类:QtCharts::QChartView
,头文件写:qchartview.h
(3)
注意:需先addSeries(),再设置坐标轴,否则显示出的线不会根据坐标轴自适应大小
QChart* c = new QChart();
//将数据添加到QLineSeries中
QLineSeries* line1 = new QLineSeries();
for(int x=0;x<ROI.grayShow.size();x++)
{line1->append(x,ROI.grayShow[x].y);
}
c->addSeries(line1); //先添加数据
c->legend()->hide(); //隐藏图例c->createDefaultAxes(); // 建立坐标轴
c->axisX()->setRange(0,640); //设置范围
c->axisX()->setGridLineVisible(true); //网格线可见
c->axisY()->setRange(0,255);
c->axisY()->setGridLineVisible(true); //网格线可见c->setTitle(QStringLiteral("中线灰度分布")); //QStringLiteral 解决中文显示乱码ui->defectWaveform->setChart(c);
Qt使用QChart制作多路虚拟示波器
https://blog.csdn.net/qq_37700564/article/details/85271907
动态实时显示多条折线图
https://blog.csdn.net/qq_41399894/article/details/87805015
Qt总结(六)-- 图表数据曲线相关推荐
- html数字数据走势图,利用HTML5实现数据曲线走势图表代码
特效描述:利用HTML5实现 数据曲线 走势图表代码.利用HTML5实现数据曲线走势图表代码 代码结构 1. 引入JS 2. HTML代码 var data = generateDayWiseTime ...
- Vue 之 echarts 图表数据可视化的基础使用(简单绘制各种图表、地图)
Vue 之 echarts 图表数据可视化的基础使用(简单绘制各种图表.地图) 目录 Vue 之 echarts 图表数据可视化的基础使用(简单绘制各种图表.地图) 一.简单介绍 二.环境搭建 三.使 ...
- Qt - QTChart绘制图表
文章目录 前言 准备工作 安装QChart组件 项目配置 主要组成部分 QChartView QChart 序列 坐标轴 图例 静态图表 动态图表 场景一 思路 效果 核心实现 场景二 思路 效果 核 ...
- Python中使用pyqtgraph绘图库实时绘制计算机CPU使用率等数据曲线
一.项目需求 获取约30台电脑的CPU使用率.CPU温度.系统盘容量.系统盘使用率.总硬盘使用率.内存使用率.GPU使用率等数据 将1中数据集中用图表的形式集中展示出来 二.设计思路 采用C/S架构( ...
- Winform开发框架之数据曲线报表
在项目开发中,往往会碰到一些非常规的需求,每次碰到这种情况,都需要花费时间来整理自己的思路,然后参考网络上其他人的实现方式或者作法,有时候可以找到一些相同的模块进行改进即可符合需求,但往往很多是需要自 ...
- 利用 Flask 动态展示 Pyecharts 图表数据的几种方法
本文将介绍如何在 web 框架 Flask 中使用可视化工具 pyecharts, 看完本教程你将掌握几种动态展示可视化数据的方法,不会的话你来找我呀- Flask 模板渲染 1. 新建一个项目fla ...
- 案例分享:Qt高频fpga采集数据压力位移速度加速度分析系统(通道配置、电压转换、采样频率、通道补偿、定时采集、距离采集,导出excel、自动XY轴、隐藏XY轴、隐藏显示通道,文件回放等等)
若该文为原创文章,转载请注明原文出处 本文章博客地址:https://hpzwl.blog.csdn.net/article/details/120345072 红胖子(红模仿)的博文大全:开发技术集 ...
- 干货!Flask 动态展示 Pyecharts 图表数据的几种方法!
本文将介绍如何在 web 框架 Flask 中使用可视化工具 pyecharts, 看完本教程你将掌握几种动态展示可视化数据的方法,不会的话你来找我呀- Flask 模板渲染 1. 新建一个项目fla ...
- Qt Xlsx使用教程、Qt操作Excel、Qt生成Excel图表、跨平台不依赖Office
文章目录 1.Qt Xlsx库简介 2. 用法①:使用Xlsx作为Qt5的附加模块 2.1 构建模块 2.2 下载QtXlsx源代码 2.3 为编译器安装Xlsx模块 2.3.1 打开 MinGW 7 ...
最新文章
- 52. N-Queens II
- 【WPF】设置TextBox内容为空时的提示文字
- Echarts数据可视化grid直角坐标系(xAxis、yAxis)
- java rdd hashmap_利用Spark Rdd生成Hfile直接导入到Hbase详解
- 华为 FusionData 发布,大数据痛点消失不见!
- 19岁少女辍学就业,却遭身价2.5亿创业公司解雇
- LAV Filter 源代码分析 2: LAV Splitter
- access 的几种更新语句:update where 与 update join where
- phpQuery中文手册(更新中)
- TypeError: empty() received an invalid combination of arguments 报错
- NPN 、PNP 三极管开关形式的典型接法(注意上下拉电阻)
- MySql重装出错, Staring the server出错,日志3306 with user root with no password...
- 【Xilinx】Zynq\MPSoc\Versal不同速度等级下的ARM主频
- Linux学习思维导图
- 网络编程之端口:端口号为什么常见是8000、8080、8888...
- varnish 缓存php,php实现监控varnish缓存服务器的状态,php监控varnish缓存
- 暑期参加CSDN编程竞赛的些许心得体会
- Python实现文字转图片
- Flask框架详细教程
- 信息系统项目管理师Part15-EAI企业应用系统集成