文章目录
- 一、示意图
- 二、实现部分代码
- 总结
一、示意图
二、实现部分代码
//! 生成测试报告
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));
}
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;
}
}
}
总结
记录下用到的不常用知识点