当前位置: 首页 > news >正文

图例QCPLegend

一、QCPLegend 概述

QCPLegend 是 QCustomPlot 中负责管理图表图例的类,用于显示图表中各元素的标识和说明。

二、功能

  • 自动/手动管理图例项

  • 支持多行列布局

  • 可自定义外观和交互

  • 支持拖拽和选择

1. 核心属性

属性类型说明
borderPenQPen图例边框的画笔样式
brushQBrush图例背景的画刷样式
fontQFont图例文本的字体
textColorQColor图例文本颜色
iconSizeQSize图例项图标尺寸(默认20x16)
iconTextPaddingint图标和文本之间的间距(默认4像素)
iconBorderPenQPen图例项图标边框的画笔
selectablePartsSelectablePart可选中部分(枚举组合)
selectedPartsSelectablePart当前选中部分
selectedBorderPenQPen选中状态的边框画笔
selectedBrushQBrush选中状态的背景画刷
selectedFontQFont选中状态的文本字体
selectedTextColorQColor选中状态的文本颜色

2. 主要方法

2.1 布局与外观

方法参数返回值说明
setVisiblebool onvoid显示/隐藏图例
setFillOrderFillOrder ordervoid设置图例项排列顺序
setRowSpacingint spacingvoid设置行间距
setColumnSpacingint spacingvoid设置列间距
setWrapint countvoid设置每行/列最大项数

2.2 图例项操作

方法参数返回值说明
addItemQCPAbstractPlottable *plottableQCPPlottableLegendItem*添加绘图项到图例
itemint indexQCPAbstractLegendItem*获取指定索引的图例项
itemWithPlottableQCPAbstractPlottable *plottableQCPPlottableLegendItem*获取关联特定绘图项的图例项
itemCount-int获取图例项数量
clearItems-void清除所有图例项

2.3 选择与交互

方法参数返回值说明
selectTestconst QPointF &pos, bool onlySelectabledouble测试点击位置是否在图例上
setSelectableSelectablePart partsvoid设置可选部分
setSelectedSelectablePart partsvoid设置选中状态

3. 信号列表

信号参数说明
selectionChangedQCPLegend::SelectablePart parts选中状态改变时触发
selectableChangedQCPLegend::SelectablePart parts可选状态改变时触发

4. 枚举类型

QCPLegend::SelectablePart

说明
spNone不可选
spLegendBox图例框可选
spItems图例项可选
spAll全部可选

QCPLegend::FillOrder

说明
foRowsFirst先填充行(默认)
foColumnsFirst先填充列

5. 基本使用示例

cpp

// 创建图例并添加到图表
QCPLegend *legend = new QCPLegend();
customPlot->plotLayout()->addElement(1, 0, legend); // 添加到底部// 设置图例样式
legend->setBorderPen(QPen(Qt::black, 1));
legend->setBrush(QBrush(Qt::white));
legend->setFont(QFont("Arial", 9));// 添加曲线到图例
QCPGraph *graph = customPlot->addGraph();
graph->setName("温度曲线");
legend->addItem(new QCPPlottableLegendItem(legend, graph));// 启用交互选择
legend->setSelectableParts(QCPLegend::spItems);
legend->setSelectedParts(QCPLegend::spNone);// 连接选择信号
connect(legend, &QCPLegend::selectionChanged, [](QCPLegend::SelectablePart parts){qDebug() << "图例选中部分:" << parts;
});

三、基本使用方法

1. 添加图例到图表

cpp

// 添加默认图例(通常已在构造函数中自动添加)
customPlot->legend->setVisible(true);// 设置图例位置
customPlot->legend->setBrush(QBrush(QColor(255,255,255,200))); // 半透明背景
customPlot->legend->setBorderPen(QPen(Qt::darkGray));
customPlot->layout()->insertRow(0); // 在上方添加空行
customPlot->legend->setFillOrder(QCPLegend::foColumnsFirst); // 填充顺序
customPlot->plotLayout()->addElement(0, 0, customPlot->legend); // 添加到布局
customPlot->plotLayout()->setMargins(QMargins(5,5,5,5)); // 设置边距

2. 添加图例项

cpp

// 自动添加图形图例
QCPGraph *graph = customPlot->addGraph();
graph->setName("正弦曲线"); // 设置名称即自动添加到图例// 手动添加自定义图例项
QCPPlottableLegendItem *legendItem = new QCPPlottableLegendItem(customPlot->legend, customPlot->graph(0));
legendItem->setText("自定义标签");// 添加纯文本图例项
QCPLegendText *textItem = new QCPLegendText(customPlot->legend);
textItem->setText("参考线");
textItem->setFont(QFont("Arial", 9));
customPlot->legend->addItem(textItem);

四、布局与样式配置

1. 行列设置

cpp

// 设置图例行数
customPlot->legend->setRowCount(2);// 设置图列数
customPlot->legend->setColumnCount(3);// 设置填充顺序(先行后列/先列后行)
customPlot->legend->setFillOrder(QCPLegend::foRowsFirst);// 设置自动换行
customPlot->legend->setWrap(4); // 每行最多4个项

2. 外观样式

cpp

// 设置图例边框
customPlot->legend->setBorderPen(QPen(Qt::black, 1));// 设置背景
customPlot->legend->setBrush(QBrush(QColor(255,255,255,230)));// 设置文本颜色
customPlot->legend->setTextColor(Qt::darkBlue);// 设置图标大小
customPlot->legend->setIconSize(20, 10);// 设置项间距
customPlot->legend->setItemSpacing(3); // 项间水平间距
customPlot->legend->setIconTextPadding(8); // 图标与文本间距

五、交互功能

1. 选择与拖拽

cpp

// 启用选择
customPlot->legend->setSelectableParts(QCPLegend::spItems); // 可选择项
customPlot->legend->setSelectedParts(QCPLegend::spNone); // 初始无选中// 启用拖拽
customPlot->legend->setDraggable(true);// 选择事件处理
connect(customPlot, &QCustomPlot::legendClick, [](QCPLegend *legend, QCPAbstractLegendItem *item, QMouseEvent *event){qDebug() << "点击了图例项:" << item->text();
});// 双击事件处理
connect(customPlot, &QCPLegend::doubleClick, [](QCPLegend *legend, QCPAbstractLegendItem *item, QMouseEvent *event){if (item) {if (auto plottableItem = dynamic_cast<QCPPlottableLegendItem*>(item)) {plottableItem->plottable()->setVisible(!plottableItem->plottable()->visible());customPlot->replot();}}
});

2. 自定义上下文菜单

cpp

// 启用上下文菜单
customPlot->setContextMenuPolicy(Qt::CustomContextMenu);connect(customPlot, &QCustomPlot::customContextMenuRequested, [=](const QPoint &pos){if (customPlot->legend->selectTest(pos, false) >= 0) {QMenu menu(customPlot);menu.addAction("隐藏图例", [=](){ customPlot->legend->setVisible(false); });menu.addAction("导出图例", [=](){ exportLegend(); });menu.exec(customPlot->mapToGlobal(pos));}
});

六、高级管理技巧

1. 分组管理

cpp

// 创建分组图例
QCPLegend *subLegend = new QCPLegend();
customPlot->plotLayout()->addElement(1, 1, subLegend); // 添加到特定位置// 将特定图形分配到子图例
QCPGraph *specialGraph = customPlot->addGraph();
specialGraph->setName("特殊曲线");
specialGraph->addToLegend(subLegend);

2. 动态更新

cpp

// 动态更新图例文本
connect(customPlot->graph(0), &QCPGraph::nameChanged, [=](const QString &newName){customPlot->legend->itemWithPlottable(customPlot->graph(0))->setText(newName + " (动态)");
});// 根据数据范围更新图例
void updateLegendWithStats(QCPGraph *graph) {bool ok;QCPRange range = graph->data()->valueRange(ok);if (ok) {graph->setName(QString("均值: %1").arg((range.lower+range.upper)/2, 0, 'f', 2));}
}

3. 自定义绘制

cpp

// 自定义图例项
class CustomLegendItem : public QCPAbstractLegendItem {
public:// 必须实现的纯虚函数virtual void draw(QCPPainter *painter) override {// 自定义绘制逻辑}virtual QSize minimumSizeHint() const override {// 返回最小尺寸}
};// 使用自定义图例项
CustomLegendItem *customItem = new CustomLegendItem(customPlot->legend);
customPlot->legend->addItem(customItem);

七、性能优化

1、限制图例项数量

cpp

// 只显示前5个图例项
for (int i=5; i<customPlot->legend->itemCount(); ++i) {customPlot->legend->itemAt(i)->setVisible(false);
}

2、简化图例样式

cpp

customPlot->legend->setAntialiased(false); // 关闭抗锯齿

3、批量操作

cpp

customPlot->legend->setVisible(false); // 先隐藏
// ...大量图例更新操作...
customPlot->legend->setVisible(true); // 最后显示

八、实用代码片段

1. 导出图例为图片

cpp

void exportLegendToImage(const QString &filename, QCPLegend *legend) {QPixmap pixmap(legend->outerRect().size().toSize());QCPPainter painter(&pixmap);legend->draw(&painter);pixmap.save(filename);
}

2. 查找特定图例项

cpp

// 通过名称查找图例项
QCPAbstractLegendItem* findLegendItem(const QString &name) {for (int i=0; i<customPlot->legend->itemCount(); ++i) {if (customPlot->legend->itemAt(i)->text() == name)return customPlot->legend->itemAt(i);}return nullptr;
}

3. 同步图例与图形可见性

cpp

// 当图形可见性改变时更新图例
connect(customPlot, &QCustomPlot::plottableVisibilityChanged, [=](QCPAbstractPlottable *plottable){if (auto item = customPlot->legend->itemWithPlottable(plottable)) {item->setTextColor(plottable->visible() ? Qt::black : Qt::gray);}
});

通过合理使用 QCPLegend,您可以创建出既美观又功能强大的图表图例系统,有效提升数据可视化的专业性和交互性。

相关文章:

  • 深入理解基线检查:网络安全的基石
  • 基于 JavaWeb 的 SpringBoot 办公 ERP 管理系统设计与实现(源码+文档+部署讲解)
  • 从浏览器地址栏输入 URL 到网页显示,这中间发生了什么?
  • [matlab]子图排版和线性回归
  • MySQL8启动失败 NET HELPMSG 3534
  • 016-C语言内存函数
  • 【HarmonyOS 5】VisionKit人脸活体检测详解
  • 【特殊场景应对3】创意岗简历骚操作:作品集链接的正确打开方式
  • 【Vue】组件通信(Props/Emit、EventBus、Provide/Inject)
  • keil5烧录后No Debug
  • (三)mac中Grafana监控Linux上的Redis(Redis_exporter安装使用)
  • 在win上安装Ubuntu安装Anaconda(linx环境)
  • 6.数据手册解读—运算放大器(三)
  • LeetCode hot 100—分割等和子集
  • 在 Node.js 中设置响应的 MIME 类型
  • RenderStage::drawInner
  • 学习笔记:黑马程序员JavaWeb开发教程(2025.3.23)
  • 计算机网络综合实验指南
  • 大模型安全吗?数据泄露与AI伦理的黑暗面!
  • ModuleNotFoundError: No module named ‘vllm.lora.peft_helper‘原因和解决方式
  • 中国在建结构第一高楼“天津117大厦”将复工,预计2027年完工
  • 石黑一雄《莫失莫忘》与“克隆人”:殖民地的记忆与行动
  • 两大跨国巨头称霸GLP-1市场,国产减肥药的机会在哪?
  • “走进电影”:虚拟现实电影产业有新进展
  • 黄山旅游:去年黄山景区累计接待进山游客492.24万人,同比增长7.6%
  • 第十个“中国航天日”活动将在沪举行:月球正面背面样品同框展出