![](https://i-blog.csdnimg.cn/blog_migrate/d36eab5efc2f939fc12b63eec5739e93.png)
头文件
widget.cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QPdfWriter>
#include<QPainter>
#include<QFile>
#include<QFileDialog>
#include<QDesktopServices>
#include<QPixmap>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
QPixmap pix;
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
修改的
![](https://i-blog.csdnimg.cn/blog_migrate/6fd602b5181c461baff12bc6ce203d1a.png)
源文件
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
QString pdfName="C://Users//Admin//soft//Qt//qt12//output.pdf";
QString pdfPath=QFileDialog::getSaveFileName(this,"导出PDF文件",pdfName,"*.pdf");//保存pdf文件路径
QFile pdffile(pdfPath);//将文件路径加载到文件对象
if(pdffile.open(QIODevice::WriteOnly))//以只写的形式打开文件
{
QPdfWriter * writer=new QPdfWriter(&pdffile);
writer->setPageSize(QPagedPaintDevice::A4);
writer->setResolution(300);//设置DPI
writer->setPageMargins(QMarginsF(20,20,20,20));//设置边距
QPainter * painter=new QPainter(writer);
painter->setFont(QFont("楷体",13));
//开始写入内容
//写入文字
int start=10;
painter->drawText(QRect(0,start,writer->width(),100),Qt::AlignCenter,"您好!Hello");
//画线
start+=120;
painter->setPen(QPen(QBrush(QColor(0,0,0)),5));
painter->drawLine(0,start,writer->width(),start);
//画图
start+=120;
QPixmap image(":/new/prefix1/支付宝.jpeg");
painter->drawPixmap(0,start,image.width(),image.height(),image);
delete painter;
painter=nullptr;
delete writer;
writer=nullptr;
pdffile.close();
}
}
修改的
![](https://i-blog.csdnimg.cn/blog_migrate/beaa90f5063f09c886845d76b771ebfc.png)
界面文件
widget.ui
![](https://i-blog.csdnimg.cn/blog_migrate/ff28796a220708a216554def518b59c4.png)
资源