Qt QCustomPlot 绘制子轴

2024-01-20 09:36
文章标签 qt 绘制 qcustomplot 子轴

本文主要是介绍Qt QCustomPlot 绘制子轴,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

抄大神杰作:QCustomplot(五)QCPAxisRect进行子绘图-CSDN博客

需求来源:试验数据需要多轴对比。

实现多Y轴、单X轴、X轴是时间轴、X轴range联动、rect之间的间距是0,每个图上有legend(这里有个疑问,每添加个rect在这个rect上添加graph,再添加legend,第一个rect上就有多个legend,其他rect上就只有一个。);

实现多Y轴、多X轴,x轴不联动。

频谱图,多Y轴,单X轴

关键代码如下,请大佬们多多指正:

//时域图
void MultiAxisWidget::recvRawData(int iRow, QString oStrLabel, QVector<double> adX, QVector<double> adY)
{QCPAxisRect* poAxisRect = new QCPAxisRect(ui->plot);poAxisRect->setAutoMargins(QCP::msNone);poAxisRect->setMargins(QMargins(100, 0, 0, 0));ui->plot->plotLayout()->addElement(iCntIndex, 0, poAxisRect);QCPAxis* poAxisX = poAxisRect->axis(QCPAxis::atBottom);QCPAxis* poAxisY = poAxisRect->axis(QCPAxis::atLeft);poAxisX->setVisible(false);poAxisY->grid()->setZeroLinePen(QPen(Qt::red));poAxisX->grid()->setSubGridVisible(true);poAxisY->grid()->setSubGridVisible(true);QList<QCPAxis*> aaxisField;aaxisField << poAxisX;poAxisRect->setRangeZoomAxes(aaxisField);poAxisY->setVisible(true);poAxisY->setLabel("振幅(mV)");auto poGraph = ui->plot->addGraph(poAxisX, poAxisY);poGraph->setName(oStrLabel);poGraph->setData(adX, adY);poGraph->setLineStyle(QCPGraph::lsLine);poGraph->setPen(QPen(Qt::blue, 2));poGraph->rescaleKeyAxis(true);poGraph->rescaleValueAxis(true, true);poGraph->rescaleAxes();auto poThread = new QThread;poGraph->setParent(nullptr);poGraph->moveToThread(poThread);poGraph->setVisible(true);QPair<QCPAxisRect*, QCPGraph*> pairContent(poAxisRect, poGraph) ;aopContent.append(pairContent);++iCntIndex;QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);//日期做X轴dateTicker->setDateTimeFormat("HH:mm:ss");//日期格式(可参考QDateTime::fromString()函数)dateTicker->setTickCount(12);switch(eAxisType){case AXIS_X_M:poAxisRect->setAutoMargins(QCP::msBottom);poAxisX->setVisible(true);poAxisX->setTicker(dateTicker);//设置X轴为时间轴break;case AXIS_X_S:if(iCntIndex == iCnt){poAxisRect->setAutoMargins(QCP::msBottom);poAxisX->setVisible(true);poAxisX->setTicker(dateTicker);//设置X轴为时间轴QTimer::singleShot(1000, this, [ = ](){this->linkage();});}break;}if(iCntIndex == iCnt){for(QPair<QCPAxisRect*, QCPGraph*> pairContent : aopContent){QCPLegend* poLegend = new QCPLegend;pairContent.first->insetLayout()->addElement(poLegend, Qt::AlignTop | Qt::AlignRight);poLegend->setLayer("legend");poLegend->addItem(new QCPPlottableLegendItem(poLegend, pairContent.second));}}ui->plot->replot();
}//频谱图
void MultiAxisWidget::recvSpectrum(QString oStrLabel, QVector<double> x, QVector<double> y, QList<double> adTargetF)
{QCPAxisRect* poAxisRect = new QCPAxisRect(ui->plot);poAxisRect->setAutoMargins(QCP::msNone);poAxisRect->setMargins(QMargins(100, 0, 0, 0));ui->plot->plotLayout()->addElement(iCntIndex, 0, poAxisRect);QCPAxis* poAxisX = poAxisRect->axis(QCPAxis::atBottom);QCPAxis* poAxisY = poAxisRect->axis(QCPAxis::atLeft);poAxisX->setVisible(false);QList<QCPAxis*> aaxisField;aaxisField << poAxisX;poAxisRect->setRangeZoomAxes(aaxisField);poAxisX->setLabel("频率(Hz)");          //X轴文字显示poAxisY->setLabel("振幅(mV)");          //Y轴文字显示poAxisY->grid()->setZeroLinePen(QPen(Qt::red));poAxisX->setRangeReversed(true);poAxisX->setScaleType(QCPAxis::stLogarithmic);poAxisY->setScaleType(QCPAxis::stLogarithmic);ui->plot->xAxis->grid()->setSubGridVisible(true);poAxisY->grid()->setSubGridVisible(true);auto poGraph = ui->plot->addGraph(poAxisX, poAxisY);poGraph->setName(oStrLabel);poGraph->setData(x, y);poGraph->setLineStyle(QCPGraph::lsLine);poGraph->setPen(QPen(Qt::blue, 2));poGraph->rescaleKeyAxis(true);poGraph->rescaleValueAxis(true, true);poGraph->rescaleAxes();auto poThread = new QThread;poGraph->setParent(nullptr);poGraph->moveToThread(poThread);poGraph->setVisible(true);for(int i = 0; i < adTargetF.count(); i++){QCPItemTracer* poTracer = new QCPItemTracer(ui->plot);poTracer->setGraphKey(adTargetF.at(i));poTracer->setInterpolating(false);poTracer->setStyle(QCPItemTracer::tsCircle);poTracer->setPen(QPen(Qt::yellow));poTracer->setBrush(Qt::red);poTracer->position->setType(QCPItemPosition::ptPlotCoords);poTracer->setSize(8);poTracer->setClipAxisRect(poAxisRect);//设置裁剪的坐标轴poTracer->setGraph(poGraph);QCPItemStraightLine* poLine = new QCPItemStraightLine(ui->plot);poLine->setPen(QPen(Qt::red, 0.5, Qt::DotLine));//垂直参考线,就是两点一线//m_pHorReffer_DG->setClipToAxisRect(false);//裁剪,让外部也要看到poLine->setClipAxisRect(poAxisRect);//设置裁剪的坐标轴poLine->point1->setAxes(poAxisRect->axis(QCPAxis::atBottom), poAxisRect->axis(QCPAxis::atLeft)); //绑定坐标poLine->point2->setAxes(poAxisRect->axis(QCPAxis::atBottom), poAxisRect->axis(QCPAxis::atLeft));poLine->point1->setCoords(adTargetF.at(i), 1);poLine->point2->setCoords(adTargetF.at(i), 2);}QPair<QCPAxisRect*, QCPGraph*> pairContent(poAxisRect, poGraph) ;aopContent.append(pairContent);++iCntIndex;if(iCntIndex == iCnt){poAxisRect->setAutoMargins(QCP::msBottom);QSharedPointer<QCPAxisTickerText> textTicker = QSharedPointer<QCPAxisTickerText>(new QCPAxisTickerText);textTicker.data()->clear();foreach(double dF, adTargetF){textTicker->addTick(dF, QString("%1").arg(dF));}poAxisX->setTicker(textTicker);poAxisX->setTickLabelRotation(34.38);poAxisX->setVisible(true);QTimer::singleShot(1000, this, [ = ](){this->linkage();});for(QPair<QCPAxisRect*, QCPGraph*> pairContent : aopContent){QCPLegend* poLegend = new QCPLegend;pairContent.first->insetLayout()->addElement(poLegend, Qt::AlignTop | Qt::AlignRight);poLegend->setLayer("legend");poLegend->addItem(new QCPPlottableLegendItem(poLegend, pairContent.second));}}ui->plot->replot();
}//多  x轴联动
void MultiAxisWidget::linkage()
{for(QPair<QCPAxisRect*, QCPGraph*> pairContent : aopContent){for(QPair<QCPAxisRect*, QCPGraph*> pairContentOther : aopContent){if(pairContentOther.first == pairContent.first){continue;}connect(pairContent.first->axis(QCPAxis::atBottom), QOverload<const QCPRange&>::of(&QCPAxis::rangeChanged),pairContentOther.first->axis(QCPAxis::atBottom), QOverload<const QCPRange&>::of(&QCPAxis::setRange));}}//鼠标双击曲线,在颜色对话框中给被点击的曲线设置颜色connect(ui->plot, &QCustomPlot::plottableDoubleClick, this, [ = ](QCPAbstractPlottable * plottable, int dataIndex, QMouseEvent * event){QColorDialog colorDialog;colorDialog.setCurrentColor(Qt::red);// 显示对话框int result = colorDialog.exec();// 检查用户是否选择了颜色if (result == QDialog::Accepted){// 获取选择的颜色QColor selectedColor = colorDialog.selectedColor();plottable->setPen(QPen(selectedColor, 2));ui->plot->replot();}});ui->plot->replot();
}

这篇关于Qt QCustomPlot 绘制子轴的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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

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

【WebGPU Unleashed】1.1 绘制三角形

一部2024新的WebGPU教程,作者Shi Yan。内容很好,翻译过来与大家共享,内容上会有改动,加上自己的理解。更多精彩内容尽在 dt.sim3d.cn ,关注公众号【sky的数孪技术】,技术交流、源码下载请添加微信号:digital_twin123 在 3D 渲染领域,三角形是最基本的绘制元素。在这里,我们将学习如何绘制单个三角形。接下来我们将制作一个简单的着色器来定义三角形内的像素

Flutter 进阶:绘制加载动画

绘制加载动画:由小圆组成的大圆 1. 定义 LoadingScreen 类2. 实现 _LoadingScreenState 类3. 定义 LoadingPainter 类4. 总结 实现加载动画 我们需要定义两个类:LoadingScreen 和 LoadingPainter。LoadingScreen 负责控制动画的状态,而 LoadingPainter 则负责绘制动画。

利用matlab bar函数绘制较为复杂的柱状图,并在图中进行适当标注

示例代码和结果如下:小疑问:如何自动选择合适的坐标位置对柱状图的数值大小进行标注?😂 clear; close all;x = 1:3;aa=[28.6321521955954 26.2453660695847 21.69102348512086.93747104431360 6.25442246899816 3.342835958564245.51365061796319 4.87

【QT】基础入门学习

文章目录 浅析Qt应用程序的主函数使用qDebug()函数常用快捷键Qt 编码风格信号槽连接模型实现方案 信号和槽的工作机制Qt对象树机制 浅析Qt应用程序的主函数 #include "mywindow.h"#include <QApplication>// 程序的入口int main(int argc, char *argv[]){// argc是命令行参数个数,argv是

Python QT实现A-star寻路算法

目录 1、界面使用方法 2、注意事项 3、补充说明 用Qt5搭建一个图形化测试寻路算法的测试环境。 1、界面使用方法 设定起点: 鼠标左键双击,设定红色的起点。左键双击设定起点,用红色标记。 设定终点: 鼠标右键双击,设定蓝色的终点。右键双击设定终点,用蓝色标记。 设置障碍点: 鼠标左键或者右键按着不放,拖动可以设置黑色的障碍点。按住左键或右键并拖动,设置一系列黑色障碍点

使用Qt编程QtNetwork无法使用

使用 VS 构建 Qt 项目时 QtNetwork 无法使用的问题 - 摘叶飞镖 - 博客园 (cnblogs.com) 另外,强烈建议在使用QNetworkAccessManager之前看看这篇文章: Qt 之 QNetworkAccessManager踏坑记录-CSDN博客 C++ Qt开发:QNetworkAccessManager网络接口组件 阅读目录 1.1 通用API函数

Qt多语种开发教程

Qt作为跨平台的开发工具,早已应用到各行各业的软件开发中。 今天讲讲,Qt开发的正序怎么做多语言开发。就是说,你设置中文,就中文显示;设置英语就英文显示,设置繁体就繁体显示,设置发育就显示法语等。 开发环境(其实多语种这块根环境没太大关系):win10,Qt.5.12.10 一.先用QtCreator创建一个简单的桌面程序 1.工程就随便命名“LanguageTest”,其他默认。 2.在设计师

Qt中window frame的影响

window frame 在创建图形化界面的时候,会创建窗口主体,上面会多出一条,周围多次一圈细边,这就叫window frame窗口框架,这是操作系统自带的。 这个对geometry的一些属性有一定影响,主要体现在Qt坐标系体系: 窗口当中包含一个按钮,这个按钮的坐标系是以父元素为参考,那么这个参考是widget本体作为参考,还是window frame作为参考,这两种参考体系都存在

【Qt】定时器事件

定时器事件 在之前学习QTimer中实现了定时器的功能,而在QTimer背后是QTimerEvent定时器事件进行支撑的。在QObject中提供了一个timeEvent这个函数。 startTimer启动定时器killTimer关闭定时器 Qt 中在进⾏窗⼝程序的处理过程中,经常要周期性的执⾏某些操作,或者制作⼀些动画效果,使⽤定 时器就可以实现。所谓定时器就是在间隔⼀定时间后,去执⾏某⼀