当前位置:网站首页>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 .
边栏推荐
- Codeforces Round #771 (Div. 2)---A-D
- VIM interval deletion note
- Speech recognition Series 1: speech recognition overview
- 公司里只有一个测试是什么体验?听听他们怎么说吧
- Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
- [array] binary search
- Agnosticism and practice makes perfect
- JSON data transfer parameters
- 2022年最新最全软件测试面试题大全
- Installing redis under Linux
猜你喜欢
RuntimeError: no valid convolution algorithms available in CuDNN
How much do you know about synchronized?
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
How does win11 turn on visual control? Win11 method of turning on visual control
In February 2022, the ranking list of domestic databases: oceanbase regained its popularity with "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
基于OpenCV实现口罩识别
How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
Connexion à distance de la tarte aux framboises en mode visionneur VNC
随机推荐
yolov5detect. Py comment
How to apply for company email when registering in company email format?
MFC gets the current time
Use of cocospods
List of major chip Enterprises
I've been interviewed. The starting salary is 16K
Installing redis under Linux
Interface switching based on pyqt5 toolbar button -1
[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
Explain in detail the process of realizing Chinese text classification by CNN
Program analysis and Optimization - 9 appendix XLA buffer assignment
Matlab 信号处理【问答笔记-1】
C MVC creates a view to get rid of the influence of layout
理想汽车×OceanBase:当造车新势力遇上数据库新势力
ArrayList analysis 2: pits in ITR, listiterator, and sublist
非路由组件之头部组件和底部组件书写
MySQL基础
JSON数据传递参数
MFC 获取当前时间
How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base