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 QCustomPlot库简介(最新推荐)

《QtQCustomPlot库简介(最新推荐)》QCustomPlot是一款基于Qt的高性能C++绘图库,专为二维数据可视化设计,它具有轻量级、实时处理百万级数据和多图层支持等特点,适用于科学计算、... 目录核心特性概览核心组件解析1.绘图核心 (QCustomPlot类)2.数据容器 (QCPDataC

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

QT6中绘制UI的两种方法详解与示例代码

《QT6中绘制UI的两种方法详解与示例代码》Qt6提供了两种主要的UI绘制技术:​​QML(QtMeta-ObjectLanguage)​​和​​C++Widgets​​,这两种技术各有优势,适用于不... 目录一、QML 技术详解1.1 QML 简介1.2 QML 的核心概念1.3 QML 示例:简单按钮

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义