当前位置:网站首页>How QT exports data to PDF files (qpdfwriter User Guide)
How QT exports data to PDF files (qpdfwriter User Guide)
2022-07-02 23:50:00 【hellokandy】
When using some text editing software , We may often encounter “ Output is PDF”, How on earth did it happen ? This article will use QPdfWriter + QPainter, Realize the QGradient::Preset Corresponding color rendering , Export to PDF file . Here is the sample code :
1、 Sample code
QString pdfName = "QGradientPreset";
pdfName.append(QDateTime::currentDateTime().toString("yyyyMMdd-HHmmss"));
pdfName.append(".pdf");
QString filePath = QFileDialog::getSaveFileName(this, QStringLiteral(" export QGradient Default "), pdfName, "*.pdf");
if (filePath.isEmpty()) {
return;
}
//
QFile pdfFile(filePath);
if (!pdfFile.open(QIODevice::WriteOnly)) {
return;
}
QPdfWriter* pdfWriter = new QPdfWriter(&pdfFile);
// initialization QPdfWriter
pdfWriter->setPageSize(QPagedPaintDevice::A4);
pdfWriter->setResolution(300); // Set up dpi( It's usually 96)
pdfWriter->setPageMargins(QMargins(30, 30, 30, 30));
// draw PDF Content
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;// Color block width
int height = 180;// Color block height
int margin = 20;// Color block spacing
// Draw title text
painter->setFont(QFont("NSimSun", 20, QFont::Normal));
painter->drawText(QRect(posLeft, posTop, pdfWidth, height),
Qt::AlignCenter,
QString::fromLocal8Bit("QGradient Preset Color value comparison chart "));
// The starting ordinate of the color block
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、 Output pdf The contents of the document

3、 other
Qt Web page links provided by assistants :https://webgradients.com/
another : The included header file required in the code , Please add your own .
边栏推荐
- The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
- 流媒体技术优化
- Integration of revolution and batch normalization
- 跨境电商如何通过打好数据底座,实现低成本稳步增长
- 万物并作,吾以观复|OceanBase 政企行业实践
- ArrayList analysis 2: pits in ITR, listiterator, and sublist
- 理想汽车×OceanBase:当造车新势力遇上数据库新势力
- 95 pages of smart education solutions 2022
- 可知论与熟能生巧
- Maybe you read a fake Tianlong eight
猜你喜欢

Difference between NVIDIA n card and amda card

第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】

直击产业落地!飞桨重磅推出业界首个模型选型工具

开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开

Connexion à distance de la tarte aux framboises en mode visionneur VNC

RuntimeError: no valid convolution algorithms available in CuDNN

Golang common settings - modify background
![[Verilog tutorial]](/img/15/d5e188a15e22fa44f1756fc492099d.jpg)
[Verilog tutorial]

MySQL基础

Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
随机推荐
cocospods 的使用
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
万物并作,吾以观复|OceanBase 政企行业实践
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
MFC文件操作
Print out mode of go
Top Devops tool chain inventory
JDBC tutorial
采用VNC Viewer方式远程连接树莓派
基于Pyqt5工具栏按钮可实现界面切换-1
Markdown basic grammar
Speech recognition Series 1: speech recognition overview
Leetcode relaxation question - day of the week
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
基于FPGA的VGA协议实现
yolov5test. Py comment
Fusion de la conversion et de la normalisation des lots
直击产业落地!飞桨重磅推出业界首个模型选型工具
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Connexion à distance de la tarte aux framboises en mode visionneur VNC