当前位置:网站首页>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/
另:代码中所需要的包含的头文件,请自行添加。
边栏推荐
- MFC 获取当前时间
- SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
- The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear
- 返回二叉树两个节点间的最大距离
- Sourcetree details
- vim区间删行注释
- Optimization of streaming media technology
- Maybe you read a fake Tianlong eight
- Go project operation method
- Win11麦克风测试在哪里?Win11测试麦克风的方法
猜你喜欢
CDN acceleration requires the domain name to be filed first
BBR encounters cubic
[Verilog tutorial]
理想汽车×OceanBase:当造车新势力遇上数据库新势力
Three solutions to frequent sticking and no response of explorer in win11 system
The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear
How does win11 turn on visual control? Win11 method of turning on visual control
YOLOX加强特征提取网络Panet分析
JDBC教程
[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
随机推荐
内网渗透 | 手把手教你如何进行内网渗透
基于FPGA的VGA协议实现
How does win11 turn on visual control? Win11 method of turning on visual control
[analysis of STL source code] imitation function (to be supplemented)
Convolution和Batch normalization的融合
What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
Hisilicon VI access video process
Golang common settings - modify background
Where is the win11 microphone test? Win11 method of testing microphone
Go basic anonymous variable
CDN acceleration requires the domain name to be filed first
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
跨境电商如何通过打好数据底座,实现低成本稳步增长
Remote connection of raspberry pie by VNC viewer
返回二叉树两个节点间的最大距离
Develop knowledge points
95页智慧教育解决方案2022
[Verilog tutorial]
Talk about memory model and memory order
面试过了,起薪16k