当前位置:网站首页>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/
另:代码中所需要的包含的头文件,请自行添加。
边栏推荐
- How to apply for company email when registering in company email format?
- 开发知识点
- CDN acceleration requires the domain name to be filed first
- Brief introduction to common sense of Zhongtai
- YOLOX加强特征提取网络Panet分析
- 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%!
- CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
- 采用VNC Viewer方式远程连接树莓派
- CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
猜你喜欢
How much do you know about synchronized?
内网渗透 | 手把手教你如何进行内网渗透
Remote connection of raspberry pie by VNC viewer
YOLOX加强特征提取网络Panet分析
How much do you know about synchronized?
The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear
Go project operation method
非路由组件之头部组件和底部组件书写
采用VNC Viewer方式远程连接树莓派
How to set automatic reply for mailbox and enterprise mailbox?
随机推荐
How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
Detailed explanation of 'viewpager' in compose | developer said · dtalk
Bean加载控制
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
How does win11 turn on visual control? Win11 method of turning on visual control
附加:token;(没写完,别看…)
Top Devops tool chain inventory
Program analysis and Optimization - 9 appendix XLA buffer assignment
程序分析与优化 - 9 附录 XLA的缓冲区指派
[array] binary search
Load balancing cluster (LBC)
(stinger) use pystinger Socks4 to go online and not go out of the network host
LINQ usage collection in C #
Agnosticism and practice makes perfect
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
Print out mode of go
Markdown basic grammar
What experience is there only one test in the company? Listen to what they say
YOLOX加强特征提取网络Panet分析
C# MVC创建一个视图摆脱布局的影响