Qt生成PDF报告

文章目录


一、示意图


二、实现部分代码

cpp 复制代码
//! 生成测试报告
void MainWindow::createPdf(QString filename, _pdf_msg_& msg, const QMap<QString, int>& ok, const QMap<QString, int>& err)
{
    //
    QDir dir;
    if(!dir.exists("PdfFiles")) qDebug()<<dir.mkdir("PdfFiles");

    //创建PDF文件
    QString file_path = filename;

    QFile pdfFile(file_path);
    bool isOpen = pdfFile.open(QIODevice::WriteOnly);

    if(!isOpen)
    {
        QMessageBox::warning(this,"error", pdfFile.errorString());
        return;
    }
    QPdfWriter *pWriter = new QPdfWriter(&pdfFile);

    pWriter->setPageSize(QPagedPaintDevice::A4);
    pWriter->setResolution(300);
    pWriter->setPageMargins(QMarginsF(30, 30, 30, 30));

    QPainter *pPainter = new QPainter(pWriter);

    QFont font[5] = {QFont("宋体", 26, 60), QFont("宋体", 26, 61), QFont("宋体", 26, QFont::Normal), QFont("宋体", 26, QFont::Normal), QFont("宋体", 26, QFont::Normal)};
    font[0].setPixelSize(86);
    font[1].setPixelSize(61);
    font[2].setPixelSize(61);
    font[3].setPixelSize(42);
    font[4].setPixelSize(54);

    //qDebug()<<pPainter->viewport().width()<<" "<<pPainter->viewport().height();

    int nPDFWidth = pPainter->viewport().width();
    int nPDFHeight = pPainter->viewport().height();

    //标题
    int y = 10;
    pPainter->setFont(font[0]);

    pPainter->drawText(QRect(0, y, nPDFWidth, 100), Qt::AlignCenter, msg.title);

    //画两条横线
    y += 140;
    pPainter->setPen(QPen(QBrush(QColor(0, 0, 0)), 5));
    pPainter->drawLine(0, y, nPDFWidth, y);
    pPainter->drawLine(0, y + 18, nPDFWidth, y + 18);

    //
    y += 60;
    pPainter->setFont(font[2]);

    pPainter->drawText(QRect(100, y, nPDFWidth/2, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("产品名称: %1").arg(msg.product_name));

    pPainter->drawText(QRect(nPDFWidth/2 + 100 , y, nPDFWidth/2 - 100, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("产品ID: %1").arg(msg.product_id));

    y += 90;

    pPainter->drawText(QRect(100, y, nPDFWidth/2, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("产品型号: %1").arg(msg.product_type));

    pPainter->drawText(QRect(nPDFWidth/2 + 100 , y, nPDFWidth/2 - 100, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("测试时间: %1").arg(msg.test_time));

    //小标题
    y += 90;
    pPainter->setFont(font[1]);
    pPainter->drawText(QRect(0, y, nPDFWidth, 80), Qt::AlignVCenter | Qt::AlignLeft, "触点比较结果");
    //画表格
    y += 110;
    QStringList list;
    list<<"序号"<<"输出点位名称"<<"结果"<<"序号"<<"输出点位名称"<<"结果";
    //
    int idx = 1;
    int row  = 0;
    int column = 0;

    if(caseType == CASETYPE::L3U)
    {
        foreach(QString s, g_RelayContact_3U)
        {
            if(s == "预留") list<<QString::number(idx)<<s<<"";
            else if(err.contains(s)) list<<QString::number(idx)<<s<<"不合格";
            else if(ok.contains(s)) list<<QString::number(idx)<<s<<"合格";
            else list<<QString::number(idx)<<s<<"未检测";
            ++idx;
        }
        row = g_RelayContact_3U.length()/2+1;
        column = 6;
    }
    else
    {
        foreach(QString s, g_RelayContact_6U)
        {
            if(s == "预留") list<<QString::number(idx)<<s<<"";
            else if(err.contains(s)) list<<QString::number(idx)<<s<<"不合格";
            else if(ok.contains(s)) list<<QString::number(idx)<<s<<"合格";
            else list<<QString::number(idx)<<s<<"未检测";
            ++idx;
        }
        row = g_RelayContact_6U.length()/2+1;
        column = 6;
    }
    pdfDrawForm(pWriter, pPainter, y, 0, row, column, 80, font[3], list);


    //!//小标题
    y += 90;
    pPainter->setFont(font[1]);
    pPainter->drawText(QRect(0, y, nPDFWidth, 80), Qt::AlignVCenter | Qt::AlignLeft, "不合格点位");

    //
    //画表格
    y += 110;
    list.clear();
    list<<"序号"<<"输出点位名称"<<"结果"<<"序号"<<"输出点位名称"<<"结果";
    //
    idx = 1;
    row  = 0;
    column = 0;

    if(caseType == CASETYPE::L3U)
    {
        for(auto  it = err.begin(); it != err.end(); ++it)
        {
            list<<QString::number(it.value())<<it.key()<<"不合格";
        }

        row = err.size()/2+1;
        column = 6;
    }
    else
    {
        for(auto it = err.begin(); it != err.end(); ++it)
        {
            list<<QString::number(it.value())<<it.key()<<"不合格";
        }
        row =err.size()/2+1;
        column = 6;
    }
    pdfDrawForm(pWriter, pPainter, y, 0, row, column, 80, font[3], list);

    //
    pPainter->setFont(font[4]);
    pPainter->drawText(QRect(50, nPDFHeight, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft, QString( "试验员: %1").arg(msg.tester_name));
    pPainter->drawText(QRect(nPDFWidth/3+50, nPDFHeight, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft, QString( "检查员签字: "));
    pPainter->drawText(QRect(nPDFWidth/3*2, nPDFHeight, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft, QString( "签字日期: "));

    //绘制完毕
    delete pPainter;
    delete pWriter;
    pdfFile.close();

    //通过其他PDF阅读器来打开PDF

    QDesktopServices::openUrl(QUrl::fromLocalFile(file_path));
}
cpp 复制代码
void MainWindow::pdfDrawForm(QPdfWriter *pWriter, QPainter *paint, int& y, int horzBorder, int row, int column, int unitHeight, QFont &font, QStringList &list)
{
    paint->setFont(font);

    paint->setPen(QPen(QBrush(QColor(0, 0, 0)), 2));

    int Width = paint->viewport().width() - horzBorder * 2;

    int unitWidth = Width / column;

    for(int i=0;i<row;i++) {
        int x = horzBorder;

        for(int j=0;j<column;j++) {
            paint->drawText(QRect(x, y, unitWidth, unitHeight), Qt::AlignCenter, list[i*column + j]);
            paint->drawRect(QRect(x, y, unitWidth, unitHeight));
            x += unitWidth;
        }

        y += unitHeight;
        if(y > paint->viewport().height())
        {
            pWriter->newPage();
            y = 10;
        }
    }
}

总结

记录下用到的不常用知识点
相关推荐
重生之我是数学王子3 小时前
QT基础 编码问题 定时器 事件 绘图事件 keyPressEvent QT5.12.3环境 C++实现
开发语言·c++·qt
----云烟----12 小时前
QT中QString类的各种使用
开发语言·qt
「QT(C++)开发工程师」18 小时前
【qt版本概述】
开发语言·qt
一路冰雨21 小时前
Qt打开文件对话框选择文件之后弹出两次
开发语言·qt
老赵的博客1 天前
QT 自定义界面布局要诀
开发语言·qt
码码哈哈0.01 天前
VSCode 2022 离线安装插件QT VSTOOl报错此扩展不能安装在任何当前安装的产品上。
ide·vscode·qt
feiyangqingyun1 天前
Qt/C++离线地图的加载和交互/可以离线使用/百度和天地图离线/支持手机上运行
c++·qt·qt天地图·qt离线地图·qt地图导航
gz94562 天前
windows下,用CMake编译qt项目,出现错误By not providing “FindQt5.cmake“...
开发语言·qt
「QT(C++)开发工程师」2 天前
Ubuntu 26.04 LTS 大升级:Qt 6 成为未来新引擎
qt
兆。2 天前
python实战案例----使用 PyQt5 构建简单的 HTTP 接口测试工具
爬虫·python·qt