【QCustomPlot04】图层(Layers)

📅 2026/7/18 8:52:47
【QCustomPlot04】图层(Layers)
【QCustomPlot04】图层Layers一、图层Layers简介二. 主要接口1 核心类2 常用方法QCustomPlot 类3、QCPLayerable 接口4、默认图层顺序从底到顶三、demo原创作者郑同学的笔记原文链接https://zhengjunxue.blog.csdn.net/article/details/155132556一、图层Layers简介QCustomPlot 是一个用于 Qt 的轻量级、高性能绘图控件广泛应用于科学数据可视化、工程监控等领域。其内部采用“图层”Layer机制来组织和管理图形元素的绘制顺序与可见性。图层系统是 QCustomPlot 渲染架构的核心组成部分。通过将不同的图形对象如坐标轴、网格线、图例、数据曲线等分配到不同的图层QCustomPlot 能够高效地控制绘制顺序、优化重绘性能并支持复杂的视觉效果例如背景/前景分离、透明叠加等。每个图层本质上是一个 QCPLayer 对象包含一组 QCPItem或其派生类如 QCPGraph、QCPAxisRect 等。QCustomPlot 默认提供多个预定义图层用户也可以动态创建、删除或重新排序图层。二. 主要接口1 核心类QCPLayer表示一个图层包含图层名称、是否可见、Z 顺序等属性。QCPLayerable所有可被分配到图层的对象的基类如 QCPGraph、QCPAxis、QCPItemText 等都继承自它。QCustomPlot主绘图控件管理所有图层。2 常用方法QCustomPlot 类// layer interface:QCPLayer*layer(constQStringname)const;QCPLayer*layer(intindex)const;QCPLayer*currentLayer()const;boolsetCurrentLayer(constQStringname);boolsetCurrentLayer(QCPLayer*layer);intlayerCount()const;booladdLayer(constQStringname,QCPLayer*otherLayernullptr,LayerInsertMode insertModelimAbove);boolremoveLayer(QCPLayer*layer);boolmoveLayer(QCPLayer*layer,QCPLayer*otherLayer,LayerInsertMode insertModelimAbove);3、QCPLayerable 接口voidsetLayer(QCPLayer*layer)或voidsetLayer(constQStringlayerName)将当前对象分配到指定图层。 QCPLayer*layer()const获取当前对象所属的图层。4、默认图层顺序从底到顶QCustomPlot 初始化时默认创建以下图层顺序不可变但可插入新图层“background”背景如背景色、背景图像“grid”网格线“main”主数据内容如曲线、散点图默认图层“axes”坐标轴刻度和标签“legend”图例“overlay”覆盖层如交互选框、临时标记三、demo#includemainwindow.h#includeui_mainwindow.h#includeQTimervoidlayerDemo(QCustomPlot*plot){// QCustomPlot plot;plot-resize(800,600);plot-setWindowTitle(QCustomPlot Layer Demo);intlayerCountplot-layerCount();qDebug()layerCountlayerCount;for(inti0;ilayerCount;i){QCPLayer*layerplot-layer(i);qDebug()layer-name();}// 添加默认图层之外的新图层plot-addLayer(behindCurves,plot-layer(main),QCustomPlot::limBelow);plot-addLayer(inFront,plot-layer(legend),QCustomPlot::limAbove);// 生成数据QVectordoublex(100),y1(100),y2(100);for(inti0;i100;i){x[i]i/10.0;y1[i]qSin(x[i]);y2[i]qCos(x[i])0.2;// 稍微偏移避免完全重叠}// 主曲线默认在 main 层autograph1plot-addGraph();graph1-setData(x,y1);graph1-setPen(QPen(Qt::blue,2));// 背景参考线放在 behindCurves 层位于主曲线之下autograph2plot-addGraph();graph2-setData(x,y2);graph2-setPen(QPen(Qt::red,1,Qt::DashLine));graph2-setLayer(behindCurves);// 关键分配到自定义图层// 前景文本标签放在 inFront 层始终在最上层QCPItemText*labelnewQCPItemText(plot);label-setPositionAlignment(Qt::AlignCenter);label-position-setType(QCPItemPosition::ptPlotCoords);label-position-setCoords(5,0.5);label-setText(Top Layer Text);label-setFont(QFont(Arial,14,QFont::Bold));label-setColor(Qt::darkGreen);label-setLayer(inFront);// 关键分配到顶层plot-xAxis-setLabel(X Axis);plot-yAxis-setLabel(Y Axis);plot-rescaleAxes();plot-replot();// 每2秒切换一次 behindCurves 图层的可见性QTimer*timernewQTimer(plot);QObject::connect(timer,QTimer::timeout,[plot](){staticboolvisibletrue;plot-layer(behindCurves)-setVisible(visible);visible!visible;plot-replot();});timer-start(2000);// plot-show();}MainWindow::MainWindow(QWidget*parent):QMainWindow(parent),ui(newUi::MainWindow){ui-setupUi(this);// 创建 QCustomPlot 实例QCustomPlot*customPlotnewQCustomPlot(this);setCentralWidget(customPlot);layerDemo(customPlot);// LegendDemo *demo new LegendDemo(this);// setCentralWidget(demo);}MainWindow::~MainWindow(){deleteui;}