当前位置:网站首页>Two methods of QT exporting PDF files
Two methods of QT exporting PDF files
2022-06-24 08:30:00 【Pepsi__】
Design software interface , It is required to export the character's information and avatar into pdf
Method 1 : Use QPrinter+QPainter.
In the project .pro Add... To the document
QT += printsupport
// export pdf
void MainWindow::on_pushButton_clicked()
{
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setResolution(300);
printer.setOutputFormat(QPrinter::PdfFormat);
// Set export pdf route
printer.setOutputFileName("D:\\test.pdf");
// Create a brush , With QPainter As a Sketchpad
QPainter painter;
painter.begin(&printer);
QFont font = ui->label_9->font();
// Bold font
font.setBold(true);
painter.setFont(font);
painter.drawText(1200,50,ui->label_9->text());
QPen pen;
pen.setColor(QColor(100,150,255));
pen.setWidth(5);
painter.setPen(pen);
// Draw a horizontal line
painter.drawLine(0,100,2400,100);
font.setBold(false);
font.setPointSize(20);
font.setFamily(" Microsoft YaHei ");
painter.setFont(font);
pen.setColor(QColor(0,0,0));
painter.setPen(pen);
painter.drawText(100,500,ui->label->text());
painter.drawText(100,800,ui->label_2->text());
painter.drawText(100,1100,ui->label_4->text());
painter.drawText(800,500,ui->label_6->text());
painter.drawText(800,800,ui->label_7->text());
painter.drawText(800,1100,ui->label_8->text());
// Get control image
QPixmap pixmap = QPixmap::grabWidget(ui->label_5,ui->label_5->rect());
QRect rect = painter.viewport();
// Set magnification
int multiple = rect.width()/3/pixmap.width();
// Zoom in multiple times
painter.scale(multiple,multiple);
painter.drawPixmap(300,60,pixmap);
painter.end();
}
Method 2 : Use QPdfWriter+QPainter
// export pdf
void MainWindow::on_pushButton_2_clicked()
{
QFile pdfFile("D:\\test.pdf");
pdfFile.open(QIODevice::WriteOnly);
QPdfWriter *pdfWriter = new QPdfWriter(&pdfFile);
pdfWriter->setPageSize(QPagedPaintDevice::A4);
//QPdfWriter As a Sketchpad
QPainter *painter = new QPainter(pdfWriter);
QFont font = ui->label_9->font();
// Bold font
font.setBold(true);
painter->setFont(font);
painter->drawText(1200,50,ui->label_9->text());
QPen pen;
pen.setColor(QColor(100,150,255));
pen.setWidth(5);
painter->setPen(pen);
// Draw a horizontal line
painter->drawLine(0,100,2400,100);
font.setBold(false);
font.setPointSize(20);
font.setFamily(" Microsoft YaHei ");
painter->setFont(font);
pen.setColor(QColor(0,0,0));
painter->setPen(pen);
painter->drawText(100,500,ui->label->text());
painter->drawText(100,800,ui->label_2->text());
painter->drawText(100,1100,ui->label_4->text());
painter->drawText(800,500,ui->label_6->text());
painter->drawText(800,800,ui->label_7->text());
painter->drawText(800,1100,ui->label_8->text());
// Get control image
QPixmap pixmap = QPixmap::grabWidget(ui->label_5,ui->label_5->rect());
QRect rect = painter->viewport();
// Set magnification
int multiple = rect.width()/3/pixmap.width();
// Zoom in multiple times
painter->scale(multiple,multiple);
painter->drawPixmap(300,60,pixmap);
// Destroy instance
delete painter;
delete pdfWriter;
// Close file
pdfFile.close();
}
Software running effect :
Click export pdf1 Use the first method to export pdf, Click export pdf2 Use the second method to export pdf
Derived pdf file :
边栏推荐
- [acnoi2022] not a structure, more like a structure
- 5分钟,客服聊天处理技巧,炉火纯青
- Nodejs redlock notes
- The article takes you to understand the security of Windows operating system and protect your computer from infringement
- Paper notes: multi label learning dm2l
- 问题3 — messageBox弹框,修改默认背景色
- 根据网络上的视频的m3u8文件通过ffmpeg进行合成视频
- [introduction to point cloud dataset]
- Transformers pretrainedtokenizer class
- 貸款五級分類
猜你喜欢

A preliminary study of IO model

Introduction to RCNN, fast RCNN and fast RCNN

12--合并两个有序链表

2022 mobile crane driver special operation certificate examination question bank and online simulation examination
![3D数学基础[十七] 平方反比定理](/img/59/bef931d96883288766fc94e38e0ace.png)
3D数学基础[十七] 平方反比定理
![Fundamentals of 3D mathematics [17] inverse square theorem](/img/59/bef931d96883288766fc94e38e0ace.png)
Fundamentals of 3D mathematics [17] inverse square theorem

Qt导出PDF文件的两种方法

LabVIEW查找n个元素数组中的质数

How to use the virtual clock of FPGA?

Question 4 - datepicker date selector, disabling two date selectors (start and end dates)
随机推荐
Review SGI STL secondary space configurator (internal storage pool) | notes for personal use
Permission model DAC ACL RBAC ABAC
Search and recommend those things
Small sample fault diagnosis - attention mechanism code - Implementation of bigru code parsing
Catégorie de prêt 5
Industrial computer anti cracking
Pat 1157: school anniversary
ZUCC_编译语言原理与编译_实验01 语言分析与简介
Five level classification of loans
你还只知道测试金字塔?
jwt(json web token)
【毕业季】你好陌生人,这是一封粉色信笺
复习SGI STL二级空间配置器(内存池) | 笔记自用
pyQt 中 QMenu 响应
2022 tea artist (intermediate) work license question bank and online simulation examination
Promise usage scenarios
2021-03-16 COMP9021第九节课笔记
Synthesize video through ffmpeg according to m3u8 file of video on the network
5分钟,客服聊天处理技巧,炉火纯青
RCNN、Fast-RCNN、Faster-RCNN介绍