news 2026/7/21 14:31:00

【QCustomPlot教程10】QCustomPlot 导出图表

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【QCustomPlot教程10】QCustomPlot 导出图表

【QCustomPlot教程10】QCustomPlot 导出图表

  • 一、简介
  • 二、demo

原创作者:郑同学的笔记
原文链接:https://zhengjunxue.blog.csdn.net/article/details/155299805

一、简介

在数据可视化应用中,将图表保存为图像或 PDF 文件是非常常见的需求。QCustomPlot 提供了简单而强大的导出接口,支持多种格式(PNG、JPG、BMP、PDF),并且可以精确控制分辨率、尺寸和质量。

boolsavePdf(constQString&fileName,intwidth=0,intheight=0,QCP::ExportPen exportPen=QCP::epAllowCosmetic,constQString&pdfCreator=QString(),constQString&pdfTitle=QString());boolsavePng(constQString&fileName,intwidth=0,intheight=0,doublescale=1.0,intquality=-1,intresolution=96,QCP::ResolutionUnit resolutionUnit=QCP::ruDotsPerInch);boolsaveJpg(constQString&fileName,intwidth=0,intheight=0,doublescale=1.0,intquality=-1,intresolution=96,QCP::ResolutionUnit resolutionUnit=QCP::ruDotsPerInch);boolsaveBmp(constQString&fileName,intwidth=0,intheight=0,doublescale=1.0,intresolution=96,QCP::ResolutionUnit resolutionUnit=QCP::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.0"pdfTitle-文档标题元数据

二、demo

#ifndefMAINWINDOW_H#defineMAINWINDOW_H#include<QMainWindow>#include"qcustomplot/qcustomplot.h"namespaceUi{classMainWindow;}classMainWindow:publicQMainWindow{Q_OBJECTpublic:explicitMainWindow(QWidget*parent=nullptr);~MainWindow();privateslots:voidonExportPng();voidonExportJpg();voidonExportBmp();voidonExportPdf();private:Ui::MainWindow*ui;QCustomPlot*customPlot;};#endif// MAINWINDOW_H
#include"mainwindow.h"#include"ui_mainwindow.h"#include<QTimer>#include"mywidget.h"MainWindow::MainWindow(QWidget*parent):QMainWindow(parent),ui(newUi::MainWindow){ui->setupUi(this);customPlot=newQCustomPlot(this);setCentralWidget(customPlot);// 创建示例图表:正弦波 + 余弦波QVector<double>x,y1,y2;for(inti=0;i<200;++i){x<<i*0.1;y1<<qSin(x[i]);y2<<qCos(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*menuBar=newQMenuBar(this);QMenu*exportMenu=menuBar->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 fileName=QFileDialog::getSaveFileName(this,"保存为 PNG","chart.png","PNG Files (*.png)");if(!fileName.isEmpty()){// 高分辨率导出:2倍缩放,宽度自动boolsuccess=customPlot->savePng(fileName,0,0,2.0,-1);QMessageBox::information(this,"提示",success?"PNG 导出成功!":"导出失败!");}}voidMainWindow::onExportJpg(){QString fileName=QFileDialog::getSaveFileName(this,"保存为 JPG","chart.jpg","JPG Files (*.jpg *.jpeg)");if(!fileName.isEmpty()){// 指定尺寸 + 高质量boolsuccess=customPlot->saveJpg(fileName,1920,1080,1.0,95);QMessageBox::information(this,"提示",success?"JPG 导出成功!":"导出失败!");}}voidMainWindow::onExportBmp(){QString fileName=QFileDialog::getSaveFileName(this,"保存为 BMP","chart.bmp","BMP Files (*.bmp)");if(!fileName.isEmpty()){// 使用当前控件尺寸boolsuccess=customPlot->saveBmp(fileName);QMessageBox::information(this,"提示",success?"BMP 导出成功!":"导出失败!");}}voidMainWindow::onExportPdf(){QString fileName=QFileDialog::getSaveFileName(this,"保存为 PDF","chart.pdf","PDF Files (*.pdf)");if(!fileName.isEmpty()){// 使用 QCP::epNoCosmetic 替代原来的 trueboolsuccess=customPlot->savePdf(fileName,0,// width (0 = auto)0,// height (0 = auto)QCP::epNoCosmetic,// ← 关键修正:不再是 bool,而是枚举!"MyApp",// pdfCreator"QCustomPlot Chart"// pdfTitle);QMessageBox::information(this,"提示",success?"PDF 导出成功!":"导出失败!");}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/21 14:30:52

终极地牢探险指南:如何用Brogue CE开启你的roguelike冒险之旅

终极地牢探险指南&#xff1a;如何用Brogue CE开启你的roguelike冒险之旅 【免费下载链接】BrogueCE Brogue: Community Edition - a community-lead fork of the much-loved minimalist roguelike game 项目地址: https://gitcode.com/gh_mirrors/br/BrogueCE Brogue C…

作者头像 李华
网站建设 2026/7/21 14:28:02

如何彻底解决Cursor试用限制?5个核心技巧让AI编程助手重获新生

如何彻底解决Cursor试用限制&#xff1f;5个核心技巧让AI编程助手重获新生 【免费下载链接】go-cursor-help 解决Cursor在免费订阅期间出现以下提示的问题: Your request has been blocked as our system has detected suspicious activity / Youve reached your trial request…

作者头像 李华
网站建设 2026/7/21 14:27:36

爱分析解析:本体平台为何是AI数据基建增速最快的细分赛道

内容来源&#xff1a;《2026爱分析AI数据基础设施市场规模报告》 发布时间&#xff1a;2026年7 月 发布机构&#xff1a;爱分析ifenxi 关键词&#xff1a;AI数据基础设施、本体平台、语义资产、AI行业分析、AI产业研究、AI原生能力建设、市场规模 摘要&#xff1a;权威AI产…

作者头像 李华
网站建设 2026/7/21 14:26:24

SpringBoot校园二手交易平台:从环境搭建到业务扩展的完整实践指南

最近在帮几个学弟学妹看毕业设计和课程设计的项目&#xff0c;发现一个挺有意思的现象&#xff1a;很多人一上来就问我&#xff0c;“有没有那种能直接跑起来、功能全、代码还好看的二手交易平台项目&#xff1f;” 他们往往不是缺技术栈&#xff0c;而是缺一个能把零散知识点串…

作者头像 李华