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