【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(constQString&name)const;QCPLayer*layer(intindex)const;QCPLayer*currentLayer()const;boolsetCurrentLayer(constQString&name);boolsetCurrentLayer(QCPLayer*layer);intlayerCount()const;booladdLayer(constQString&name,QCPLayer*otherLayer=nullptr,LayerInsertMode insertMode=limAbove);boolremoveLayer(QCPLayer*layer);boolmoveLayer(QCPLayer*layer,QCPLayer*otherLayer,LayerInsertMode insertMode=limAbove);3、QCPLayerable 接口
voidsetLayer(QCPLayer*layer)或voidsetLayer(constQString&layerName)将当前对象分配到指定图层。 QCPLayer*layer()const获取当前对象所属的图层。4、默认图层(顺序从底到顶)
QCustomPlot 初始化时默认创建以下图层(顺序不可变,但可插入新图层):
- “background”:背景(如背景色、背景图像)
- “grid”:网格线
- “main”:主数据内容(如曲线、散点图,默认图层)
- “axes”:坐标轴刻度和标签
- “legend”:图例
- “overlay”:覆盖层(如交互选框、临时标记)
三、demo
#include"mainwindow.h"#include"ui_mainwindow.h"#include<QTimer>voidlayerDemo(QCustomPlot*plot){// QCustomPlot plot;plot->resize(800,600);plot->setWindowTitle("QCustomPlot Layer Demo");intlayerCount=plot->layerCount();qDebug()<<"layerCount="<<layerCount;for(inti=0;i<layerCount;++i){QCPLayer*layer=plot->layer(i);qDebug()<<layer->name();}// 添加默认图层之外的新图层plot->addLayer("behindCurves",plot->layer("main"),QCustomPlot::limBelow);plot->addLayer("inFront",plot->layer("legend"),QCustomPlot::limAbove);// 生成数据QVector<double>x(100),y1(100),y2(100);for(inti=0;i<100;++i){x[i]=i/10.0;y1[i]=qSin(x[i]);y2[i]=qCos(x[i])+0.2;// 稍微偏移避免完全重叠}// 主曲线(默认在 "main" 层)autograph1=plot->addGraph();graph1->setData(x,y1);graph1->setPen(QPen(Qt::blue,2));// 背景参考线(放在 behindCurves 层,位于主曲线之下)autograph2=plot->addGraph();graph2->setData(x,y2);graph2->setPen(QPen(Qt::red,1,Qt::DashLine));graph2->setLayer("behindCurves");// 关键:分配到自定义图层// 前景文本标签(放在 inFront 层,始终在最上层)QCPItemText*label=newQCPItemText(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*timer=newQTimer(plot);QObject::connect(timer,&QTimer::timeout,[plot](){staticboolvisible=true;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*customPlot=newQCustomPlot(this);setCentralWidget(customPlot);layerDemo(customPlot);// LegendDemo *demo = new LegendDemo(this);// setCentralWidget(demo);}MainWindow::~MainWindow(){deleteui;}