头文件
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QPixmap>
#include<QPdfWriter>
#include<QPainter>
#include<QFile>
#include<QFileDialog>
#include<QDesktopServices>
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();
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
修改的
源文件
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()
{
pix.load(":/new/prefix1/支付宝.jpeg");
ui->label_4->setPixmap(pix);
ui->label_4->setScaledContents(true);
}
void Widget::on_pushButton_2_clicked()
{
QString pdfName="C://Users//Admin//soft//Qt//qt10//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);
//文字
painter->drawText(QRect(0,170,writer->width(),100),Qt::AlignLeft,"姓名:xxx 性别:x 年龄:xx");
painter->drawText(QRect(0,220,writer->width(),100),Qt::AlignLeft,"毕业院校:xxxx学院 专业:软件工程");
painter->drawText(QRect(0,280,writer->width(),100),Qt::AlignLeft,"籍贯:xxx");
//画图
start+=120;
QPixmap image(":/new/prefix1/支付宝.jpeg");
painter->drawPixmap(1000,start,image.width(),image.height(),image);
//画线
start+=220;
painter->setPen(QPen(QBrush(QColor(0,0,0)),5));
painter->drawLine(0,start,writer->width(),start);
delete painter;
painter=nullptr;
delete writer;
writer=nullptr;
pdffile.close();
}
}
修改的
界面文件
widget.ui
(意外丢失了)