首页 > 代码库 > 使用QPrinter生成pdf或用打印机打印数据
使用QPrinter生成pdf或用打印机打印数据
1.使用html生成表格
QString wesiOperateWidget::MakeDataToHtml(){ //表头 //html = "<table width=100% border=1 cellspacing=0 text-align=center style=border-collapse:collapse>"; //html += "<tr>"; //html += "<th>编号</th>"; //html += "<th>姓名</th>"; //html += "<th>头部成绩</th>"; //html += "<th>胸部成绩</th>"; //html += "</tr>"; ////数据 //html += "<tr>"; //html += "<td align=center>1</td>"; //html += "<td align=center>赵六</td>"; //html += "<td align=center>100</td>"; //html += "<td align=center>400</td>"; //html += "</tr>"; //html += "</table>"; QString html; html = "<table width=100% border=1 cellspacing=0 text-align=center style=border-collapse:collapse>"; html += "<tr>"; html += "<th>编号</th>"; html += "<th>姓名</th>"; html += "<th>头部成绩</th>"; html += "<th>胸部成绩</th>"; html += "</tr>"; QStringList dataStringList; QStringList rowInfoList; html += "<tr>"; foreach(QString str, dataStringList) { rowInfoList = str.split(","); for(int i = 0; i < 8; i += 2) { if(rowInfoList[i] == "number") { html = html + "<td align=center>" + rowInfoList[i + i] + "</td>"; } else if(rowInfoList[i] == "name") { html = html + "<td align=center>" + rowInfoList[i + i] + "</td>"; } else if(rowInfoList[i] == "headscore") { html = html + "<td align=center>" + rowInfoList[i + i] + "</td>"; } else if(rowInfoList[i] == "bodyscore") { html = html + "<td align=center>" + rowInfoList[i + i] + "</td>"; } } } html += "</tr>"; html += "</table>"; return html;}
2.生成pdf文档
QPrinter printer; printer.setPageSize(QPrinter::A4); printer.setOutputFormat(QPrinter::PdfFormat); QString filepath = QFileDialog::getSaveFileName(this, tr("保存为..."), tr("D://成绩.pdf"), tr("PDF格式(*.pdf)")); printer.setOutputFileName(filepath); QTextDocument textDocument; textDocument.setHtml(MakeDataToHtml()); textDocument.print(&printer); QMessageBox::about(this, tr("提示"), tr("保存成功"));
3.使用打印机打印
1 QPrinter printer;2 printer.setPageSize(QPrinter::A4);3 QPrintDialog printDialog(&printer, this);4 if (printDialog.exec()){5 QTextDocument textDocument;6 textDocument.setHtml(MakeDataToHtml());7 textDocument.print(&printer);8 }
使用QPrinter生成pdf或用打印机打印数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。