当前位置:网站首页>QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
2022-07-02 22:42:00 【hellokandy】
在平时使用一些文字编辑软件时,我们可能经常会遇到“输出为PDF”,那究竟是如何做到的呢?本文将使用QPdfWriter + QPainter,实现把 QGradient::Preset 对应的颜色效果图,导出为PDF文件。以下是示例代码:
1、示例代码
QString pdfName = "QGradientPreset";
pdfName.append(QDateTime::currentDateTime().toString("yyyyMMdd-HHmmss"));
pdfName.append(".pdf");
QString filePath = QFileDialog::getSaveFileName(this, QStringLiteral("导出QGradient预设值"), pdfName, "*.pdf");
if (filePath.isEmpty()) {
return;
}
//
QFile pdfFile(filePath);
if (!pdfFile.open(QIODevice::WriteOnly)) {
return;
}
QPdfWriter* pdfWriter = new QPdfWriter(&pdfFile);
//初始化QPdfWriter
pdfWriter->setPageSize(QPagedPaintDevice::A4);
pdfWriter->setResolution(300); //设置dpi(一般是96)
pdfWriter->setPageMargins(QMargins(30, 30, 30, 30));
//绘制PDF内容
QPainter* painter = new QPainter(pdfWriter);
painter->setRenderHint(QPainter::Antialiasing);
int pdfWidth = painter->viewport().width();
int pdfHeight = painter->viewport().height();
int colorIndex = 0;
int posTop = 0;
int posLeft = 0;
int width = 530;//色块宽度
int height = 180;//色块高度
int margin = 20;//色块间距
//绘制标题文字
painter->setFont(QFont("NSimSun", 20, QFont::Normal));
painter->drawText(QRect(posLeft, posTop, pdfWidth, height),
Qt::AlignCenter,
QString::fromLocal8Bit("QGradient Preset 色值对照图"));
//色块的起始纵坐标
posTop += height;
//paint begin
while (colorIndex < 180)//QGradient::Preset::NumPresets - 1
{
colorIndex++;
//
QGradient::Preset clr = QGradient::Preset(colorIndex);
painter->fillRect(QRect(posLeft, posTop, width, height), clr);
//
painter->setFont(QFont("NSimSun", 14, QFont::Normal));
painter->drawText(QRect(posLeft, posTop, width, height),
Qt::AlignCenter,
QString::fromLocal8Bit("Preset: %1").arg(colorIndex));
posLeft += (width + margin);
int posRight = posLeft + width;
if (posRight >= pdfWidth)
{
posLeft = 0;
//
posTop += (height + margin);
int posButtom = posTop + height;
if (posButtom >= pdfHeight)
{
pdfWriter->newPage();
posTop = 0;
}
}
}
//paint end
//delete it
delete painter;
delete pdfWriter;
pdfFile.close();
//
QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
2、输出的pdf文件内容
3、其他
Qt助手提供的网页链接:https://webgradients.com/
另:代码中所需要的包含的头文件,请自行添加。
边栏推荐
- 流媒体技术优化
- MarkDown基本语法
- Intranet penetration | teach you how to conduct intranet penetration hand in hand
- [error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
- 简述中台的常识
- 面试过了,起薪16k
- Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
- Talk about memory model and memory order
- Win11麦克风测试在哪里?Win11测试麦克风的方法
- How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
猜你喜欢
Connexion à distance de la tarte aux framboises en mode visionneur VNC
Use redis to realize self increment serial number
购买完域名之后能干什么事儿?
Mapper agent development
采用VNC Viewer方式遠程連接樹莓派
Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
PR FAQ, what about PR preview video card?
How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
Go basic data type
Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
随机推荐
Eight honors and eight disgraces of the programmer version~
程序分析与优化 - 9 附录 XLA的缓冲区指派
Connexion à distance de la tarte aux framboises en mode visionneur VNC
【直播预约】数据库OBCP认证全面升级公开课
List of major chip Enterprises
leetcode 650. 2 keys keyboard with only two keys (medium)
JDBC tutorial
How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
Brief introduction to common sense of Zhongtai
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
面试过了,起薪16k
Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
(毒刺)利用Pystinger Socks4上线不出网主机
Wechat applet basic learning (wxss)
Win11如何开启目视控制?Win11开启目视控制的方法
A single element in an ordered array -- Valentine's Day mental problems
接口自动化覆盖率统计——Jacoco使用
“一个优秀程序员可抵五个普通程序员!”
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
Use of cocospods