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 中 isHidden 和 isVisible 的区别与使用小结

《Qt中isHidden和isVisible的区别与使用小结》Qt中的isHidden()和isVisible()方法都用于查询组件显示或隐藏状态,然而,它们有很大的区别,了解它们对于正确操... 目录1. 基础概念2. 区别清见3. 实际案例4. 注意事项5. 总结1. 基础概念Qt 中的 isHidd

QT移植到RK3568开发板的方法步骤

《QT移植到RK3568开发板的方法步骤》本文主要介绍了QT移植到RK3568开发板的方法步骤,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录前言一、获取SDK1. 安装依赖2. 获取SDK资源包3. SDK工程目录介绍4. 获取补丁包二

Qt把文件夹从A移动到B的实现示例

《Qt把文件夹从A移动到B的实现示例》本文主要介绍了Qt把文件夹从A移动到B的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录如何移动一个文件? 如何移动文件夹(包含里面的全部内容):如何删除文件夹:QT 文件复制,移动(

Qt实现发送HTTP请求的示例详解

《Qt实现发送HTTP请求的示例详解》这篇文章主要为大家详细介绍了如何通过Qt实现发送HTTP请求,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、添加network模块2、包含改头文件3、创建网络访问管理器4、创建接口5、创建网络请求对象6、创建一个回复对

Qt 中集成mqtt协议的使用方法

《Qt中集成mqtt协议的使用方法》文章介绍了如何在工程中引入qmqtt库,并通过声明一个单例类来暴露订阅到的主题数据,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一,引入qmqtt 库二,使用一,引入qmqtt 库我是将整个头文件/源文件都添加到了工程中进行编译,这样 跨平台

基于Qt Qml实现时间轴组件

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

使用Python绘制蛇年春节祝福艺术图

《使用Python绘制蛇年春节祝福艺术图》:本文主要介绍如何使用Python的Matplotlib库绘制一幅富有创意的“蛇年有福”艺术图,这幅图结合了数字,蛇形,花朵等装饰,需要的可以参考下... 目录1. 绘图的基本概念2. 准备工作3. 实现代码解析3.1 设置绘图画布3.2 绘制数字“2025”3.3

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

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

使用Python绘制可爱的招财猫

《使用Python绘制可爱的招财猫》招财猫,也被称为“幸运猫”,是一种象征财富和好运的吉祥物,经常出现在亚洲文化的商店、餐厅和家庭中,今天,我将带你用Python和matplotlib库从零开始绘制一... 目录1. 为什么选择用 python 绘制?2. 绘图的基本概念3. 实现代码解析3.1 设置绘图画

Python绘制土地利用和土地覆盖类型图示例详解

《Python绘制土地利用和土地覆盖类型图示例详解》本文介绍了如何使用Python绘制土地利用和土地覆盖类型图,并提供了详细的代码示例,通过安装所需的库,准备地理数据,使用geopandas和matp... 目录一、所需库的安装二、数据准备三、绘制土地利用和土地覆盖类型图四、代码解释五、其他可视化形式1.