QT 使用QPdfWriter和QPainter绘制PDF文件

QT如何生产pdf文件,网上有许多文章介绍,我也是看了网上的文章,看他们的代码,自己琢磨琢磨,才有了本编博客;

其他什么就不详细说了,本篇博客介绍的QPdfWriter和QPainter绘制PDF文件;对pdf这里是绘制出来的,没有什么规范的格式,都是通过xy坐标绘制出来的。

QPdfWriter类设置pdf的基础设置;QPainter类绘制文本,图片矩形等;

以下代码参考博客:Qt中使用QPdfWriter类结合QPainter类绘制并输出PDF文件-CSDN博客

自己稍微做了调整了修改!

反正得自己会QPainter绘制,否则你无法绘制pdf文件出来!!!

头文件:

#include <QPdfWriter>
#include <QDesktopServices>
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QtPrintSupport>

一、步骤

绘制pdf有以下步骤:

1.选择需要导出的pdf文件路径;

2.创建pdf文件;

3.创建生成pdf类,作为绘图设备;

4.绘制PDF;

5.查看绘制好的pdf。

void Widget::exportPdf()
{//一、选择保存pdf文件路径QString sPath = QFileDialog::getSaveFileName(this, tr("另存为"), "/", tr("Text Files (*.pdf)"));if(sPath.isEmpty()){return;}qDebug() << sPath;//二、创建pdf文件QFile pdfFile(sPath);pdfFile.open(QIODevice::WriteOnly);//三、创建生成pdf类,作为绘图设备QPdfWriter *pPdfWriter = new QPdfWriter(&pdfFile);pPdfWriter->setResolution(300);      // 将打印设备的分辨率设置为屏幕分辨率pPdfWriter->setPageSize(QPagedPaintDevice::A4);             // 设置纸张为A4纸pPdfWriter->setPageMargins(QMarginsF(30, 30, 30, 30));      // 设置页边距 顺序是:左上右下//四、开始绘制PDF//paintPdf(pPdfWriter);delete pPdfWriter;pdfFile.close();//通过其它PDF阅读器来打开刚刚绘制的PDFQDesktopServices::openUrl(QUrl::fromLocalFile(sPath));
}

二、效果展示

三、代码实现

#include "widget.h"
#include "ui_widget.h"#include <QFileDialog>
#include <QStandardPaths>
#include <QPdfWriter>
#include <QDebug>
#include <QDesktopServices>
#include <QMessageBox>
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QtPrintSupport>#include <pdfgenerator.h>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);createPDF();
}Widget::~Widget()
{delete ui;
}void Widget::createPDF()
{int y = 0;QString path = "/home/UOS/Desktop/3.pdf";PdfGenerator *pdfGenerator = new PdfGenerator;bool flag = pdfGenerator->setFileName(path);if (!flag) {return ;}int nPdfWidth = pdfGenerator->getPdfWidth();pdfGenerator->beginPage();QPixmap p;p.load(":/007.jpg");int pHeight = pdfGenerator->drawImage(QRectF(0, y, 150, 150), p);y += pHeight + 50;// 绘制表格y += 100;QFont f = QFont("宋体", 18, 36);QColor blackColor = QColor(0,0,0);pdfGenerator->drawRect(QRectF(0, y, nPdfWidth, 200), blackColor, 2, QColor(100, 0, 200, 50));pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 200), "这是标题文本!", QFont("宋体", 32, 64));// 绘制科目y += 200;int nClassWidthFlag = nPdfWidth / (8);int nClassWidth = nClassWidthFlag;QList<QString> classList;classList << "语文" << "数学" << "英语" << "历史" << "政治" << "地理" << "音乐" << "体育";int classx = 0;for (int i = 0; i < classList.count(); i++) {pdfGenerator->drawRect(QRectF(classx, y, nClassWidth, 100), blackColor, 2);pdfGenerator->drawText(QRectF(classx, y, nClassWidth, 100), classList.at(i), QFont("宋体", 12, 20), QColor(200, 100, 100), Qt::AlignCenter);classx += nClassWidth;if (7 == i + 1) {int n = nPdfWidth - (classx + nClassWidth);nClassWidth += n;}}// 绘制个人信息y += 100;int nPersonDescriptionWidth = nClassWidthFlag * 2;QList<QList<QString>> descriptionList;QList<QString> list;list << "小米" << "男" << "15";descriptionList << list;list.clear();list << "小明" << "男" << "20";descriptionList << list;list.clear();list << "小红" << "女" << "21";descriptionList << list;list.clear();list << "姓名" << "性别" << "年龄";int nPersonDescriptionHeight = 120 + descriptionList.count() * 100;pdfGenerator->drawRect(QRect(0, y, nPersonDescriptionWidth, nPersonDescriptionHeight));pdfGenerator->drawText(QRect(0, y, nPersonDescriptionWidth, nPersonDescriptionHeight), "个人信息",QFont("宋体", 20, 40), QColor(100, 100, 255));int nMessageX = nPersonDescriptionWidth;int nMessageWidth = nPersonDescriptionWidth;for (int i = 0; i < list.count(); i++) {pdfGenerator->drawRect(QRect(nMessageX, y, nMessageWidth, 120));pdfGenerator->drawText(QRect(nMessageX, y, nMessageWidth, 120), list.at(i), QFont("宋体", 18, 30), QColor(200, 50, 50));nMessageX += nPersonDescriptionWidth;if (list.count()-1 == i + 1) {int n = nPdfWidth - (nMessageX + nPersonDescriptionWidth);nMessageWidth += n;}}y += 120;int nPersonX = nPersonDescriptionWidth;int nPersonWidth = nPersonDescriptionWidth;for (int i = 0; i < descriptionList.count(); ++i) {QList<QString> list = descriptionList.at(i);for (int j = 0; j < list.count(); ++j) {pdfGenerator->drawRect(QRect(nPersonX, y, nPersonWidth, 100));pdfGenerator->drawText(QRect(nPersonX, y, nPersonWidth, 100), list.at(j), pdfGenerator->getBaseFont());nPersonX += nPersonDescriptionWidth;if (list.count()-1 == j + 1) {int n = nPdfWidth - (nPersonX + nPersonDescriptionWidth);nPersonWidth += n;}}y += 100;nPersonX = nPersonDescriptionWidth;nPersonWidth = nPersonDescriptionWidth;}// 绘制详细信息descriptionList.clear();list.clear();list << "小明" << "swim" << "擅长蛙泳" << "170cm";descriptionList << list;list.clear();list << "小红" << "dance" << "街舞鼻祖" << "160cm";descriptionList << list;list.clear();list << "小黄" << "run" << "短跑小王子" << "166cm";descriptionList << list;list.clear();list << "小绿" << "jump" << "跳高运动员" << "196cm";descriptionList << list;list.clear();list << "姓名" << "爱好" << "特点" << "身高";int nDetailedInformationWidth = nClassWidthFlag * 2;int nDetailedInformationHeight = 120 + descriptionList.count() * 100;pdfGenerator->drawRect(QRect(0, y, nDetailedInformationWidth, nDetailedInformationHeight));pdfGenerator->drawText(QRect(0, y, nDetailedInformationWidth, nDetailedInformationHeight), "详细信息",QFont("宋体", 20, 40), QColor(100, 100, 255));int nInformationTotalWidth = nPdfWidth - nDetailedInformationWidth;int nInformationOneWidth = nInformationTotalWidth / descriptionList.count();int nDetailedX = nDetailedInformationWidth;int nDetailedWidth = nInformationOneWidth;for (int i = 0; i < list.count(); i++) {pdfGenerator->drawRect(QRect(nDetailedX, y, nDetailedWidth, 120));pdfGenerator->drawText(QRect(nDetailedX, y, nDetailedWidth, 120), list.at(i), QFont("宋体", 20, 30));nDetailedX += nDetailedWidth;if (list.count()-1 == i + 1) {int n = nPdfWidth - (nDetailedX + nDetailedWidth);nDetailedWidth += n;}}y += 120;int nInformationX = nDetailedInformationWidth;int nInformationWidth = nInformationOneWidth;for (int i = 0; i < descriptionList.count(); ++i) {QList<QString> list = descriptionList.at(i);for (int j = 0; j < list.count(); ++j) {pdfGenerator->drawRect(QRect(nInformationX, y, nInformationWidth, 100));pdfGenerator->drawText(QRect(nInformationX, y, nInformationWidth, 100), list.at(j),pdfGenerator->getBaseFont(), QColor(200,0,0));nInformationX += nInformationWidth;if (list.count()-1 == j + 1) {int n = nPdfWidth - (nInformationX + nInformationWidth);nInformationWidth += n;}}y += 100;nInformationX = nDetailedInformationWidth;nInformationWidth = nInformationOneWidth;}y += 50;pdfGenerator->drawPolygon(QRect(600, y, 800, 600));pdfGenerator->drawRect(QRect(600, y, 800, 600));// 换页//    if(y + 100 >= pdfGenerator->getPdfHeight()) {//        pdfGenerator->newPage();//        y = 10;//    }pdfGenerator->newPage();y = 0;/************************************************************************************************************/// 外部大矩形框pdfGenerator->drawRect(QRect(0, y, nPdfWidth, pdfGenerator->getPdfHeight()), QColor(0,0,0), 8);// 左上角m级f = pdfGenerator->getBaseFont();f.setBold(true);f.setPointSize(13);pdfGenerator->drawText(QRectF(10, y, 360, 100), "m级:非m", f, QColor(0,0,0), Qt::AlignLeft);// 页码int pageX = 900;f = pdfGenerator->getBaseFont("宋体", 12, 0);pdfGenerator->drawText(QRectF(pageX, y, 500, 100), "第 1 页   Page", f, QColor(0,0,0), Qt::AlignLeft);y += 80;pdfGenerator->drawText(QRectF(pageX, y, 1444, 100), "共 2 页   This report includes page", f, QColor(0,0,0), Qt::AlignLeft);y += 100;// 标题1f = pdfGenerator->getBaseFont("宋体", 20, 8);pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 120), "嘻嘻嘻这是一份关于检测相关的世界的详细报告哈哈", f, QColor(0,0,0), Qt::AlignCenter);y += 120;// 标题1英语f = pdfGenerator->getBaseFont("Times New Roman", 11, 8);QString english = "abc abc He he he, this is a detailed report about the world related to testing. Ha ha. abc abc";pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 120), english, f, QColor(0,0,0), Qt::AlignCenter);y += 150;// 标题2f = pdfGenerator->getBaseFont("宋体", 30, 8);pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 130), "一二三四五六报告", f, QColor(0,0,0), Qt::AlignCenter);y += 160;// 标题2英语f = pdfGenerator->getBaseFont("Times New Roman", 20, 12);pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 130), "Tihs is a Report", f, QColor(0,0,0), Qt::AlignCenter);y += 160;// 报告编号f = pdfGenerator->getBaseFont("宋体", 10, 0);pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 50), "报告编号:( 2025 )报告的 第 15 号", f, QColor(0,0,0), Qt::AlignCenter);y += 80;// Report No.f = pdfGenerator->getBaseFont("Times New Roman", 10, 0);pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 50), "Report No.", f, QColor(0,0,0), Qt::AlignCenter);int nameX = 200;    // 距离左边的距离int lineX = 500;    // 横线距离左边的距离int lineWidth = 1322;    // 横线宽度y += 100;for (int i = 0; i < 6; i++) {int tmpY = y + 30;// 你的名称f = pdfGenerator->getBaseFont();pdfGenerator->drawText(QRectF(nameX, y, 250, 60), "你的名称:", f, QColor(0,0,0), Qt::AlignLeft);y += 80;// 英文f = pdfGenerator->getBaseFont("Times New Roman", 10, 0);pdfGenerator->drawText(QRectF(nameX, y, 250, 60), "Your name", f, QColor(0,0,0), Qt::AlignLeft);y += 70;// 画横线f = pdfGenerator->getBaseFont();pdfGenerator->drawLine(QPointF(lineX, y), QPointF(lineX + lineWidth, y));// 画文本pdfGenerator->drawText(QRectF(lineX, tmpY, lineWidth, 80), "这是名字呀", f, QColor(0,0,0), Qt::AlignCenter);y += 30;// 自动换页处理
//        if(y + 200 >= pdfGenerator->getPdfHeight()) {
//            pdfGenerator->newPage();
//            y = 10;
//        }}y += 50;// 签发人f = pdfGenerator->getBaseFont();pdfGenerator->drawText(QRectF(nameX, y, 400, 60), "签发人:(签字)", f, QColor(0,0,0), Qt::AlignLeft);int dataX = 1111;// 发证日期f = pdfGenerator->getBaseFont();pdfGenerator->drawText(QRectF(dataX, y, 400, 60), "发证日期:", f, QColor(0,0,0), Qt::AlignLeft);y += 90;// 签发人 英文f = pdfGenerator->getBaseFont("Times New Roman", 10);pdfGenerator->drawText(QRectF(nameX, y, 400, 60), "Signature of leader", f, QColor(0,0,0), Qt::AlignLeft);// 发证日期 英文f = pdfGenerator->getBaseFont("Times New Roman", 10);pdfGenerator->drawText(QRectF(dataX, y, 400, 60), "lssued date", f, QColor(0,0,0), Qt::AlignLeft);y += 200;// 发证单位f = pdfGenerator->getBaseFont();pdfGenerator->drawText(QRectF(dataX, y, 600, 60), "发证单位:(盖章位置)", f, QColor(0,0,0), Qt::AlignLeft);y += 90;// 发证单位 英文f = pdfGenerator->getBaseFont("Times New Roman", 10);pdfGenerator->drawText(QRectF(dataX, y, 400, 60), "lssued by(stamp)", f, QColor(0,0,0), Qt::AlignLeft);y += 200;// 地址f = pdfGenerator->getBaseFont("宋体", 10);f.setBold(true);pdfGenerator->drawText(QRectF(nameX, y, 600, 66), "地址(Add):", f, QColor(0,0,0), Qt::AlignLeft);f = pdfGenerator->getBaseFont("宋体", 10);pdfGenerator->drawText(QRectF(nameX + 330, y, 800, 66), "广东省广州市天河区xx街道xx村xx号", f, QColor(0,0,0), Qt::AlignLeft);// 邮编f = pdfGenerator->getBaseFont("宋体", 10);f.setBold(true);pdfGenerator->drawText(QRectF(nameX + 1100, y, 600, 66), "邮编(Post Code):", f, QColor(0,0,0), Qt::AlignLeft);f = pdfGenerator->getBaseFont("宋体", 10);pdfGenerator->drawText(QRectF(nameX + 1550, y, 300, 66), "123456", f, QColor(0,0,0), Qt::AlignLeft);y += 100;// 电话f = pdfGenerator->getBaseFont("宋体", 10);f.setBold(true);pdfGenerator->drawText(QRectF(nameX, y, 600, 66), "电话(Tel):", f, QColor(0,0,0), Qt::AlignLeft);f = pdfGenerator->getBaseFont("宋体", 10);pdfGenerator->drawText(QRectF(nameX + 330, y, 800, 66), "002-123456789", f, QColor(0,0,0), Qt::AlignLeft);// 传真f = pdfGenerator->getBaseFont("宋体", 10);f.setBold(true);pdfGenerator->drawText(QRectF(nameX + 800, y, 600, 66), "传真(Fax):", f, QColor(0,0,0), Qt::AlignLeft);f = pdfGenerator->getBaseFont("宋体", 10);pdfGenerator->drawText(QRectF(nameX + 1050, y, 300, 66), "002-123456789", f, QColor(0,0,0), Qt::AlignLeft);y += 100;// 电子邮箱f = pdfGenerator->getBaseFont("宋体", 10);f.setBold(true);pdfGenerator->drawText(QRectF(nameX, y, 600, 66), "电子信箱(E-mail):", f, QColor(0,0,0), Qt::AlignLeft);f = pdfGenerator->getBaseFont("宋体", 10);pdfGenerator->drawText(QRectF(nameX + 400, y, 800, 66), "youxiang666@qq.com", f, QColor(0,0,0), Qt::AlignLeft);pdfGenerator->endPage();// 通过其它PDF阅读器来打开刚刚绘制的PDFQDesktopServices::openUrl(QUrl::fromLocalFile(path));
}

四、源码分享

pdfgenerator.h

#ifndef PDF_GENERATOR_H
#define PDF_GENERATOR_H#include <QObject>
#include <QPdfWriter>
#include <QPainter>
#include <QFont>
#include <QImage>
#include <QPageSize>
#include <QFile>class PdfGenerator : public QObject {Q_OBJECT
public:explicit PdfGenerator(const QString &fileName, QPagedPaintDevice::PageSize size = QPagedPaintDevice::PageSize::A4, QObject *parent = nullptr);explicit PdfGenerator(QObject *parent = nullptr);~PdfGenerator();qreal getPdfWidth();qreal getPdfHeight();/*** @brief getBaseFont   获得基本的字体* @param family* @param pointSize* @param weight* @return*/QFont getBaseFont(const QString &family = "宋体", int pointSize = 12, int weight = -1);/*** @brief setMargins    设置pdf边距,分别是 左-上-右-下* @param left* @param top* @param right* @param bottom*/void setMargins(qreal left, qreal top, qreal right, qreal bottom);/*** @brief setResolution     设置分辨率,一般为300* @param dpi*/void setResolution(int dpi = 300);/*** @brief newPage           新建下一页*/void newPage();/*** @brief beginPage         开始绘画* @return      成功返回true;失败返回false*/bool beginPage();/*** @brief endPage           结束会话* @return      成功返回true;失败返回false*/bool endPage();/*** @brief setFileName       设置pdf文件名,内部会open* @param fileName* @param size* @return*/bool setFileName(const QString &fileName, QPagedPaintDevice::PageSize size = QPagedPaintDevice::PageSize::A4);/*** @brief drawLine  绘制线段* @param start     起始坐标* @param end       结束坐标* @param color     线段颜色* @param width     线段宽度*/void drawLine(const QPointF &start, const QPointF &end, const QColor &color = QColor(0,0,0), qreal width = 2);/*** @brief drawText  绘制文字* @param rect      绘制的位置(矩形)* @param text      绘制的文本* @param font      绘制字体* @param color     字体颜色* @param align     对齐*/void drawText(const QRectF &rect, const QString &text, const QFont &font, const QColor &color = QColor(0,0,0), Qt::Alignment align = Qt::AlignCenter);/*** @brief drawImage             绘制图片* @param rect                  绘制的位置(矩形)* @param imagePath             图片路径*/int drawImage(const QRectF &rect, const QString &imagePath);int drawImage(const QRectF &rect, const QPixmap &pixmap);int drawImage(const QRectF &rect, const QImage &image);/*** @brief drawRect          绘制矩形* @param rect              绘制的位置(矩形)* @param borderColor       边框的颜色* @param borderWidth       边框宽度* @param fillColor         填充的颜色,默认透明,不填充*/void drawRect(const QRectF &rect, const QColor &borderColor = QColor(0,0,0), qreal borderWidth = 2, const QColor &fillColor = Qt::transparent);/*** @brief drawEllipse       绘制椭圆* @param rect              绘制的位置(矩形)* @param borderColor       边框的颜色* @param borderWidth       边框宽度* @param fillColor         填充的颜色,默认透明,不填充*/void drawEllipse(const QRectF &rect, const QColor &borderColor = QColor(0,0,0), qreal borderWidth = 2, const QColor &fillColor = Qt::transparent);/*** @brief drawPolygon       画三角形,三角形在矩形区域内* @param rect              绘制的位置(矩形)* @param borderColor       边框的颜色* @param borderWidth       边框宽度* @param fillColor         填充的颜色,默认透明,不填充*/void drawPolygon(const QRectF &rect, const QColor &borderColor = QColor(0,0,0), qreal borderWidth = 2, const QColor &fillColor = Qt::transparent);// 想设计其他绘制接口继续往下加private:QPdfWriter *m_writer = nullptr;QPainter *m_painter = nullptr;/// pdf可绘制区域QRect m_pageRect;/// pdf文件QFile m_pdfFile;
};#endif  // PDF_GENERATOR_H

pdfgenerator.cpp

#include "pdfgenerator.h"
#include <QtDebug>PdfGenerator::PdfGenerator(const QString &fileName, QPagedPaintDevice::PageSize size, QObject *parent) : QObject (parent)
{m_pdfFile.setFileName(fileName);m_writer = new QPdfWriter(&m_pdfFile);m_writer->setPageSize(size);                // 设置纸张m_writer->setResolution(300);               // 设置分辨率m_writer->setPageMargins(QMarginsF(20, 20, 20, 20), QPageLayout::Millimeter);   // 设置页边距m_pageRect = m_writer->pageLayout().paintRectPixels(m_writer->resolution());// 计算可绘制区域m_pageRect = QRect(0, 0, m_writer->width(), m_writer->height());if(!m_pdfFile.open(QIODevice::WriteOnly))return ;}PdfGenerator::PdfGenerator(QObject *parent) : QObject (parent)
{}PdfGenerator::~PdfGenerator()
{if (m_painter) {if (m_painter->isActive()){m_painter->end();}delete m_painter;}if (m_writer) {m_writer->deleteLater();}
}qreal PdfGenerator::getPdfWidth()
{return m_pageRect.width();
}qreal PdfGenerator::getPdfHeight()
{return m_pageRect.height();
}QFont PdfGenerator::getBaseFont(const QString &family, int pointSize, int weight)
{QFont f(family, pointSize, weight);return f;
}void PdfGenerator::setMargins(qreal left, qreal top, qreal right, qreal bottom)
{m_writer->setPageMargins(QMarginsF(left, top, right, bottom), QPageLayout::Millimeter);m_pageRect = m_writer->pageLayout().paintRectPixels(m_writer->resolution()); // 更新绘制区域
}void PdfGenerator::setResolution(int dpi)
{m_writer->setResolution(dpi);
}void PdfGenerator::newPage()
{// 创建新页m_writer->newPage();
}bool PdfGenerator::beginPage()
{bool bRet = false;if(nullptr == m_painter){m_painter = new QPainter(m_writer);}//启用抗锯齿m_painter->setRenderHint(QPainter::Antialiasing);if (nullptr != m_painter){m_painter->begin(m_writer);//m_painter->reset(new  QPainter(m_writer.data()));bRet = m_painter->isActive();}qDebug() << "beginPage bRet is " << bRet;return bRet;
}bool PdfGenerator::endPage() {if (m_painter && m_painter->isActive()){m_painter->end();m_writer->deleteLater();m_pdfFile.close();return true;}m_pdfFile.close();return false;
}bool PdfGenerator::setFileName(const QString &fileName, QPagedPaintDevice::PageSize size)
{m_pdfFile.setFileName(fileName);// 打开创建并以写的方式打开文件if(!m_pdfFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {return false;}m_writer = new QPdfWriter(&m_pdfFile);m_writer->setPageSize(size);                // 设置纸张m_writer->setResolution(300);               // 设置分辨率m_writer->setPageMargins(QMarginsF(20, 20, 20, 20), QPageLayout::Millimeter);   // 设置页边距m_pageRect = m_writer->pageLayout().paintRectPixels(m_writer->resolution());// 计算可绘制区域m_pageRect = QRect(0, 0, m_writer->width(), m_writer->height());return true;
}// 绘制线段
void PdfGenerator::drawLine(const QPointF &start, const QPointF &end, const QColor &color, qreal width)
{if (!m_painter->isActive())return;m_painter->save();m_painter->setPen(QPen(color, width));m_painter->drawLine(start, end);m_painter->restore();
}// 绘制文本(支持对齐)
void PdfGenerator::drawText(const QRectF &rect, const QString &text, const QFont &font, const QColor &color, Qt::Alignment align)
{if (!m_painter->isActive())return;m_painter->save();m_painter->setFont(font);m_painter->setPen(color);m_painter->drawText(rect, static_cast<int>(align), text);m_painter->restore();
}// 绘制图片(根据大小比例,来放大缩小图片)
int PdfGenerator::drawImage(const QRectF &rect, const QString &imagePath)
{if (!m_painter->isActive())return -10;QPixmap pixmap;if (!pixmap.load(imagePath)) {return -1;}int nPdfWidth = m_pageRect.width();int imageBorder = rect.width();float x = (float)(nPdfWidth - imageBorder * 2) / (float)pixmap.width();pixmap = pixmap.scaled(nPdfWidth - imageBorder * 2, x * pixmap.height(), Qt::IgnoreAspectRatio);    //根据大小比例,来放大缩小图片m_painter->drawPixmap(imageBorder, static_cast<int>(rect.y()), pixmap);return pixmap.height();
}int PdfGenerator::drawImage(const QRectF &rect, const QPixmap &pixmap)
{if (!m_painter->isActive())return -1;if (pixmap.isNull())return -1;int nPdfWidth = m_pageRect.width();int imageBorder = rect.width();float x = (float)(nPdfWidth - imageBorder * 2) / (float)pixmap.width();QPixmap pixmapTmp = pixmap.scaled(nPdfWidth - imageBorder * 2, x * pixmap.height(), Qt::IgnoreAspectRatio);    //根据大小比例,来放大缩小图片m_painter->drawPixmap(imageBorder, static_cast<int>(rect.y()), pixmapTmp);return pixmapTmp.height();
}int PdfGenerator::drawImage(const QRectF &rect, const QImage &image)
{if (!m_painter->isActive())return -1;if (image.isNull())return -1;int nPdfWidth = m_pageRect.width();int imageBorder = rect.width();float x = (float)(nPdfWidth - imageBorder * 2) / (float)image.width();QImage imageTmp = image.scaled(nPdfWidth - imageBorder * 2, x * image.height(), Qt::IgnoreAspectRatio);    //根据大小比例,来放大缩小图片m_painter->drawImage(rect, image);return image.height();
}// 绘制矩形(支持填充)
void PdfGenerator::drawRect(const QRectF &rect, const QColor &borderColor, qreal borderWidth, const QColor &fillColor)
{if (!m_painter->isActive())return;m_painter->save();m_painter->setBrush(QBrush(fillColor));     // 填充m_painter->setPen(QPen(borderColor, borderWidth));m_painter->drawRect(rect);m_painter->restore();
}// 绘制椭圆
void PdfGenerator::drawEllipse(const QRectF &rect, const QColor &borderColor, qreal borderWidth, const QColor &fillColor)
{if (!m_painter->isActive())return;m_painter->save();m_painter->setBrush(QBrush(fillColor));m_painter->setPen(QPen(borderColor, borderWidth));m_painter->drawEllipse(rect);m_painter->restore();
}void PdfGenerator::drawPolygon(const QRectF &rect, const QColor &borderColor, qreal borderWidth, const QColor &fillColor)
{if (!m_painter->isActive())return;QPen pen(borderColor, borderWidth); // 底边线宽为5m_painter->setPen(pen);QBrush brush(fillColor);m_painter->setBrush(brush);QPoint points[3] = {QPoint(rect.x(), rect.y() + rect.height()), // 左下角点QPoint(rect.x() + rect.width(), rect.y() + rect.height()), // 右下角点QPoint((rect.width() + rect.x() + rect.x()) / 2, rect.y()) // 上顶点};m_painter->drawPolygon(points, 3);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/81708.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

企业标准信息公共服务平台已开放标准通编辑器访问入口

标准通 数字化标准编辑器 专业、高效、便捷 企业标准信息公共服务平台 近日&#xff0c;企业标准信息公共服务平台已开放标准通编辑器访问入口&#xff0c;可进入官网指定版块使用&#xff01; 核心功能亮点 解决企业痛点 传统标准编制&#xff0c;需反复核对格式、逐条…

【Hadoop】--HA高可用搭建--3.2.2

修改环境配置文件 hadoop-env.sh # 在文件末尾添加以下内容&#xff1a; # java_home记得修改 export JAVA_HOME/usr/java/jdk1.8.0xxxx export HDFS_NAMENODE_USERroot export HDFS_DATANODE_USERroot export HDFS_ZKFC_USERroot export HDFS_JOURNALNODE_USERroot export YA…

【skywalking】index“:“skywalking_metrics-all“},“status“:404}

skywalking 启动报错 java.lang.RuntimeException: {"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index [skywalking_metrics-all]","resource.t ype":"inde…

【Android】从垂直同步到屏幕刷新机制(一)

【Android】从垂直同步到屏幕刷新机制 本文参考以及部分图片来源&#xff1a; 垂直同步_小科普&#xff1a;“垂直同步”究竟是什么&#xff1f;-CSDN博客 “终于懂了” 系列&#xff1a;Android屏幕刷新机制—VSync、Choreographer 全面理解&#xff01;-腾讯云开发者社区-腾讯…

ACL完全解析:从权限管理到网络安全的核心防线

访问控制列表&#xff08;ACL&#xff09;是一种用于管理资源访问权限的核心安全机制&#xff0c;广泛应用于操作系统和网络设备中。以下是对ACL的详细解析&#xff1a; 1. 基本概念 定义&#xff1a;ACL是由多个访问控制条目&#xff08;ACE&#xff09;组成的列表&#xff0…

代码审计-php框架开发,实战tp项目,打击微交易,源码获取,扩大战果

实战&#xff0c;不安全写法引发的注入 这个bc靶场源码没有&#xff0c;看老师演示 打开很明显的tp框架源码 拿到tp框架之后第一步&#xff0c;搜索版本信息5.0.5 两个思路 1.代码的不安全写法 2.版本自身存在的漏洞 全局搜索where看看也没有不安全的 哎&#xff1f;&…

大模型的实践应用43-基于Qwen3(32B)+LangChain框架+MCP+RAG+传统算法的旅游行程规划系统

大家好,我是微学AI,今天给大家介绍一下大模型的实践应用43-基于Qwen3(32B)+LangChain框架+MCP+RAG+传统算法的旅游行程规划系统。本报告将阐述基于大模型Qwen3(32B)、LangChain框架、MCP协议、RAG技术以及传统算法构建的智能旅游行程规划系统。该系统通过整合多种技术优势,实…

Jsoup库和Apache HttpClient库有什么区别?

Jsoup 和 Apache HttpClient 是两个功能不同的库&#xff0c;它们在 Java 开发中被广泛使用&#xff0c;但用途和功能有明显的区别&#xff1a; Jsoup 用途&#xff1a;Jsoup 是一个用于解析 HTML 文档的库。它提供了非常方便的方法来抓取和解析网页内容&#xff0c;提取和操作…

腾讯云存储原理

我们来详细展开你提到的两个核心结构概念&#xff1a; 一、“基于分布式文件系统 对象存储技术” 是什么&#xff1f; 1. 分布式文件系统&#xff08;DFS&#xff09;基础 分布式文件系统是一种支持将数据分布在多个存储节点上、并对上层用户透明的文件系统。腾讯云COS虽然是…

python fastapi + react, 写一个图片 app

1. 起因&#xff0c; 目的: 上厕所的时候&#xff0c;想用手机查看电脑上的图片&#xff0c;但是又不想点击下载。此app 应运而生。 2. 先看效果 单击图片&#xff0c;能放大图片 3. 过程: 过程很枯燥。有时候&#xff0c; 有一堆新的想法。 但是做起来太麻烦&#xff0c;…

Kubernetes控制平面组件:Kubelet详解(五):切换docker运行时为containerd

云原生学习路线导航页&#xff08;持续更新中&#xff09; kubernetes学习系列快捷链接 Kubernetes架构原则和对象设计&#xff08;一&#xff09;Kubernetes架构原则和对象设计&#xff08;二&#xff09;Kubernetes架构原则和对象设计&#xff08;三&#xff09;Kubernetes控…

QT6 源(111):阅读与注释菜单栏 QMenuBar,进行属性与成员函数测试,信号与槽函数测试,并给出源码

&#xff08;1&#xff09; &#xff08;2&#xff09; &#xff08;3&#xff09; &#xff08;4&#xff09; &#xff08;5&#xff09; &#xff08;6&#xff09; &#xff08;7&#xff09;以下源代码来自于头文件 qmenubar . h &#xff1a; #ifndef QMENUBAR_H #defi…

Leetcode 3552. Grid Teleportation Traversal

Leetcode 3552. Grid Teleportation Traversal 1. 解题思路2. 代码实现 题目链接&#xff1a;3552. Grid Teleportation Traversal 1. 解题思路 这一题的话核心就是一个广度优先遍历&#xff0c;我们只需要从原点开始&#xff0c;一点点考察其所能到达的位置&#xff0c;直至…

2023CCPC河南省赛暨河南邀请赛个人补题ABEFGHK

Dashboard - 2023 CCPC Henan Provincial Collegiate Programming Contest - Codeforces 过题难度&#xff1a;A H F G B K E 铜奖&#xff1a; 2 339 银奖&#xff1a; 3 318 金奖&#xff1a; 5 523 A: 直接模拟 // Code Start Here int t;cin >> t;while(t-…

如何用Python批量解压ZIP文件?快速解决方案

如何用Python批量解压ZIP文件&#xff1f;快速解决方案 文章目录 **如何用Python批量解压ZIP文件&#xff1f;快速解决方案**代码结果详细解释 话不多说&#xff0c;先上干货&#xff01;&#xff01;&#xff01; 代码 import os import zipfiledef unzip_file(dir_path: str…

Spring Boot 的高级特性与经典的设计模式应用

目录 1. 设计模式在 Spring Boot 中的应用 1.1 单例模式&#xff1a;Bean 管理与全局实例 1.1.1 Spring 中的单例 Bean 1.1.2 自定义单例实现 1.1.3 单例模式的优势 1.2 工厂模式&#xff1a;动态创建 Bean 1.2.1 Spring 的工厂方法 1.2.2 自定义工厂类 1.2.3 工厂模式…

在Excel中使用函数公式时,常见错误对应不同的典型问题

在Excel中使用函数公式时&#xff0c;常见错误对应不同的典型问题 1. #DIV/0!&#xff08;除以零错误&#xff09;2. #N/A&#xff08;值不可用&#xff09;3. #NAME?&#xff08;名称错误&#xff09;4. #NULL!&#xff08;空交集错误&#xff09;5. #NUM!&#xff08;数值错…

【cursor疑惑】cursor续杯后使用agent对话时,提示“需要pro或商业订阅的用户才能使用“

背景 cursor的pro会员体验过期了&#xff0c;想再次体验deepseek、Claude等agent对话提示:“免费版本不可以使用agent对话功能(英文忘记截图了&#xff0c;大意是这样)”。 处理方法 Step-1&#xff1a;再次续杯cursor的pro会员14天体验 详情&#xff0c;见&#xff1a;【c…

解决qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed

可以参考&#xff1a;解决qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed-CSDN博客 讲的是程序执行目录下可能缺少了&#xff1a; libssl-1_1-x64.dll 和 libcrypto-1_1-x64.dll 库文件&#xff0c;将其复制到可执行文件exe的同级目录下即可…

白杨SEO:不到7天,白杨SEO博客网站百度搜索显示和排名恢复正常!顺带说说上海线下GEO聚会分享和播客红利

大家好&#xff0c;我是白杨SEO&#xff0c;专注SEO十年以上&#xff0c;全网SEO流量实战派&#xff0c;AI搜索优化研究者。 5月开始&#xff0c;明显就忙起来了&#xff0c;不管是个人陪跑还是企业顾问&#xff0c;不管是需要传统SEO还是新媒体流量&#xff0c;还是当下这个A…