Qt下将文件输出为图片文件

来源:互联网 发布:云技术与大数据 编辑:程序博客网 时间:2024/05/21 22:54

Qt下将文件输出为png,jpg格式的图片文件有两种方法:一是QPixmap自带的save()函数,即先绘图后将QPixmap保存为图片文件。二是用先生成QFile后往QFile中写入文件。本文先介绍第一种。

第一种具体代码及注释如下:

QPixmap exportImage(1200,1000);    exportImage.fill(Qt::white);    QPainter painter(&exportImage);    //抓取坐标轴    QPixmap axisPixmap=QWidget::grab(QRect(ui->qcustomplot->x(),ui->qcustomplot->y()+10,ui->qcustomplot->frameGeometry().width(),ui->qcustomplot->frameGeometry().height()+10)); //抓取界面widget区域,可以抓取任意控件区域,Qt5推荐新的API QWidget::grab    painter.drawPixmap(10,10,axisPixmap);/*************************************以下部分可自定义绘图***********************************************/    painter.end();/*************************************保存文件***********************************************/    QString file_path = QFileDialog::getSaveFileName(this, tr("Save File"),                               "/home/jana/untitled.png",                               tr("Images (*.png *.xpm *.jpg)"));    if(!file_path.isEmpty())    {        //如果没有写后缀就自动加上        if(QFileInfo(file_path).suffix().isEmpty())            file_path.append(".png");        exportImage.save(file_path);    }    else        QMessageBox::warning(this,tr("Path"),QString::fromLocal8Bit("未选择保存文件名"));


 
原创粉丝点击