QCustomPlot基础教程(八)——QCustomPlot将绘制的图形保存导出

本文介绍了QCustomPlot提供的四种图像保存接口,包括saveBmp、saveJpg、savePng和savePdf,以及如何根据文件后缀动态保存不同格式。通过实例展示了如何在用户点击按钮后,根据用户选择的文件类型进行相应的图像保存操作。
摘要由CSDN通过智能技术生成

一、函数介绍

QCustomPlot提供了四种常用的save接口,其格式如下:

saveBmp(const QString &fileName, int width, int height, double scale, int resolution, QCP::ResolutionUnit resolutionUnit);
saveJpg(const QString &fileName, int width, int height, double scale, int quality, int resolution, QCP::ResolutionUnit resolutionUnit);
savePng(const QString &fileName, int width, int height, double scale, int quality, int resolution, QCP::ResolutionUnit resolutionUnit);
savePdf(const QString &fileName, int width, int height, QCP::ExportPen exportPen, const QString &pdfCreator, const QString &pdfTitle);

二、代码示例

首先在UI界面中创建一个按钮控件,其ObjectName为pbn_save,然后在mainwindow.cpp中写入如下代码:

bool MainWindow::on_pbn_save_clicked()
{
    QString filename = QFileDialog::getSaveFileName();

    if( filename == "" ){
        QMessageBox::information(this,"fail","保存失败");
         return false;
     }
     if( filename.endsWith(".png") ){
         QMessageBox::information(this,"success","成功保存为png文件");
         return ui->customPlot->savePng( filename, ui->customPlot->width(), ui->customPlot->height() );

     }
     if( filename.endsWith(".jpg")||filename.endsWith(".jpeg") ){
         QMessageBox::information(this,"success","成功保存为jpg文件");
         return ui->customPlot->saveJpg( filename, ui->customPlot->width(), ui->customPlot->height() );

     }
     if( filename.endsWith(".bmp") ){
         QMessageBox::information(this,"success","成功保存为bmp文件");
         return ui->customPlot->saveBmp( filename, ui->customPlot->width(), ui->customPlot->height() );

     }
     if( filename.endsWith(".pdf") ){
         QMessageBox::information(this,"success","成功保存为pdf文件");
         return ui->customPlot->savePdf( filename, ui->customPlot->width(), ui->customPlot->height() );

     }
    else{
     //否则追加后缀名为.png保存文件
         QMessageBox::information(this,"success","保存成功,已默认保存为png文件");
     return ui->customPlot->savePng(filename.append(".png"), ui->customPlot->width(), ui->customPlot->height() );

    }
}

对上述代码进行简单介绍,首先定义QString类型的filename存放用户想要保存的文件名称,然后检测文件名后缀,如果是png,jpg,jpeg,bmp,pdf结尾,则图像保存为该格式,否则默认图像保存为png格式文件,并弹出相应保存成功或保存失败的提示信息。

参考:https://vimsky.com/examples/detail/cpp-ex—QCustomPlot-savePdf-method.html

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wendy_ya

您的鼓励将是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值