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.pswp.cn/bicheng/81708.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/81708.shtml
英文地址,請注明出處:http://en.pswp.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…