当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Convolution和Batch normalization的融合
What experience is there only one test in the company? Listen to what they say
Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
Dishes launcher small green program and directory management (efficiency tool)
How does win11 turn on visual control? Win11 method of turning on visual control
采用VNC Viewer方式远程连接树莓派
Intranet penetration | teach you how to conduct intranet penetration hand in hand
67 page overall planning and construction plan for a new smart city (download attached)
Bean加载控制
Implementation of VGA protocol based on FPGA
随机推荐
[proteus simulation] 51 MCU +lcd12864 push box game
JSON data transfer parameters
Three solutions to frequent sticking and no response of explorer in win11 system
【STL源码剖析】仿函数(待补充)
Mapper agent development
基于OpenCV实现口罩识别
JSON数据传递参数
Request and response
95 pages of smart education solutions 2022
Explain in detail the process of realizing Chinese text classification by CNN
Writing of head and bottom components of non routing components
Integration of revolution and batch normalization
Arduino - character judgment function
List of major chip Enterprises
数据集-故障诊断:西储大学轴承的各项数据以及数据说明
[live broadcast appointment] database obcp certification comprehensive upgrade open class
CDN acceleration requires the domain name to be filed first
PHP get real IP
67页新型智慧城市整体规划建设方案(附下载)
PR FAQ, what about PR preview video card?