【QCustomPlot教程10】QCustomPlot 导出图表 【QCustomPlot教程10】QCustomPlot 导出图表一、简介二、demo原创作者郑同学的笔记原文链接https://zhengjunxue.blog.csdn.net/article/details/155299805一、简介在数据可视化应用中将图表保存为图像或 PDF 文件是非常常见的需求。QCustomPlot 提供了简单而强大的导出接口支持多种格式PNG、JPG、BMP、PDF并且可以精确控制分辨率、尺寸和质量。boolsavePdf(constQStringfileName,intwidth0,intheight0,QCP::ExportPen exportPenQCP::epAllowCosmetic,constQStringpdfCreatorQString(),constQStringpdfTitleQString());boolsavePng(constQStringfileName,intwidth0,intheight0,doublescale1.0,intquality-1,intresolution96,QCP::ResolutionUnit resolutionUnitQCP::ruDotsPerInch);boolsaveJpg(constQStringfileName,intwidth0,intheight0,doublescale1.0,intquality-1,intresolution96,QCP::ResolutionUnit resolutionUnitQCP::ruDotsPerInch);boolsaveBmp(constQStringfileName,intwidth0,intheight0,doublescale1.0,intresolution96,QCP::ResolutionUnit resolutionUnitQCP::ruDotsPerInch);通用参数适用于所有格式fileName-保存的文件路径和名称 width-输出宽度像素0使用当前宽度 height-输出高度像素0使用当前高度 scale-缩放因子PNG/JPG/BMP专用1.0原始大小 resolution-分辨率数值PNG/JPG/BMP专用 resolutionUnit-分辨率单位PNG/JPG/BMP专用格式特有参数PNG/JPG 特有 quality-压缩质量-1默认0-100质量级别PDF 特有exportPen-导出笔设置 类型QCP::ExportPen 默认值QCP::epAllowCosmetic 可选值 QCP::epAllowCosmetic-允许使用装饰性笔cosmetic pens QCP::epNoCosmetic-不使用装饰性笔确保打印时线条宽度正确 说明控制笔的导出方式影响线条在PDF中的显示效果 pdfCreator-创建者信息 类型constQString默认值空字符串 说明PDF文档的创建者元数据 示例My Application v1.0pdfTitle-文档标题元数据二、demo#ifndefMAINWINDOW_H#defineMAINWINDOW_H#includeQMainWindow#includeqcustomplot/qcustomplot.hnamespaceUi{classMainWindow;}classMainWindow:publicQMainWindow{Q_OBJECTpublic:explicitMainWindow(QWidget*parentnullptr);~MainWindow();privateslots:voidonExportPng();voidonExportJpg();voidonExportBmp();voidonExportPdf();private:Ui::MainWindow*ui;QCustomPlot*customPlot;};#endif// MAINWINDOW_H#includemainwindow.h#includeui_mainwindow.h#includeQTimer#includemywidget.hMainWindow::MainWindow(QWidget*parent):QMainWindow(parent),ui(newUi::MainWindow){ui-setupUi(this);customPlotnewQCustomPlot(this);setCentralWidget(customPlot);// 创建示例图表正弦波 余弦波QVectordoublex,y1,y2;for(inti0;i200;i){xi*0.1;y1qSin(x[i]);y2qCos(x[i]);}customPlot-addGraph();customPlot-graph(0)-setData(x,y1);customPlot-graph(0)-setPen(QPen(Qt::blue,2));customPlot-addGraph();customPlot-graph(1)-setData(x,y2);customPlot-graph(1)-setPen(QPen(Qt::red,2));customPlot-xAxis-setLabel(X);customPlot-yAxis-setLabel(Y);customPlot-xAxis-setRange(0,20);customPlot-yAxis-setRange(-1.2,1.2);customPlot-legend-setVisible(true);customPlot-legend-setFont(QFont(Helvetica,9));customPlot-graph(0)-setName(sin(x));customPlot-graph(1)-setName(cos(x));customPlot-rescaleAxes();customPlot-replot();// 添加菜单栏导出选项QMenuBar*menuBarnewQMenuBar(this);QMenu*exportMenumenuBar-addMenu(导出(E));exportMenu-addAction(导出为 PNG,this,MainWindow::onExportPng);exportMenu-addAction(导出为 JPG,this,MainWindow::onExportJpg);exportMenu-addAction(导出为 BMP,this,MainWindow::onExportBmp);exportMenu-addAction(导出为 PDF,this,MainWindow::onExportPdf);setMenuBar(menuBar);}MainWindow::~MainWindow(){deleteui;}voidMainWindow::onExportPng(){QString fileNameQFileDialog::getSaveFileName(this,保存为 PNG,chart.png,PNG Files (*.png));if(!fileName.isEmpty()){// 高分辨率导出2倍缩放宽度自动boolsuccesscustomPlot-savePng(fileName,0,0,2.0,-1);QMessageBox::information(this,提示,success?PNG 导出成功:导出失败);}}voidMainWindow::onExportJpg(){QString fileNameQFileDialog::getSaveFileName(this,保存为 JPG,chart.jpg,JPG Files (*.jpg *.jpeg));if(!fileName.isEmpty()){// 指定尺寸 高质量boolsuccesscustomPlot-saveJpg(fileName,1920,1080,1.0,95);QMessageBox::information(this,提示,success?JPG 导出成功:导出失败);}}voidMainWindow::onExportBmp(){QString fileNameQFileDialog::getSaveFileName(this,保存为 BMP,chart.bmp,BMP Files (*.bmp));if(!fileName.isEmpty()){// 使用当前控件尺寸boolsuccesscustomPlot-saveBmp(fileName);QMessageBox::information(this,提示,success?BMP 导出成功:导出失败);}}voidMainWindow::onExportPdf(){QString fileNameQFileDialog::getSaveFileName(this,保存为 PDF,chart.pdf,PDF Files (*.pdf));if(!fileName.isEmpty()){// 使用 QCP::epNoCosmetic 替代原来的 trueboolsuccesscustomPlot-savePdf(fileName,0,// width (0 auto)0,// height (0 auto)QCP::epNoCosmetic,// ← 关键修正不再是 bool而是枚举MyApp,// pdfCreatorQCustomPlot Chart// pdfTitle);QMessageBox::information(this,提示,success?PDF 导出成功:导出失败);}}

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/9/29 2:52:50
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/29 2:52:51
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/9/29 1:29:30
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/9/29 1:39:24
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/28 17:20:49
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/29 2:52:53

日新闻

周新闻