当前位置:网站首页>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
- Brief introduction to common sense of Zhongtai
- Go basic anonymous variable
- 富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
- Agnosticism and practice makes perfect
- Interpretation of new plug-ins | how to enhance authentication capability with forward auth
- Flexible combination of applications is a false proposition that has existed for 40 years
- Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
- How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
- Request and response
猜你喜欢

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

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

35 pages dangerous chemicals safety management platform solution 2022 Edition

CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)

(stinger) use pystinger Socks4 to go online and not go out of the network host

采用VNC Viewer方式遠程連接樹莓派

Go project operation method

Realization of mask recognition based on OpenCV
![[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)](/img/75/3f6203410dd2754e578e0baaaa9aef.png)
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)

【直播预约】数据库OBCP认证全面升级公开课
随机推荐
Detailed explanation of 'viewpager' in compose | developer said · dtalk
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
Analyze ad654: Marketing Analytics
I've been interviewed. The starting salary is 16K
QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
Speech recognition Series 1: speech recognition overview
(stinger) use pystinger Socks4 to go online and not go out of the network host
How much do you know about synchronized?
RTP 接发ps流工具改进(二)
基于FPGA的VGA协议实现
Container runtime analysis
Interface switching based on pyqt5 toolbar button -1
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
MFC gets the current time
@How to use bindsinstance in dagger2
PR FAQ, what about PR preview video card?
Practical series - free commercial video material library
Go project operation method
yolov5detect. Py comment
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)