【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 导出成功!":"导出失败!");}}