QPrinter打印类怎么自定义纸张大小
需要借用它生成pdf文档,但是大小总是通过枚举去设置的。有个setPageSize(QPrinter::Custom)又不知道下一步该 怎么设置 。设置成Custom还是A4默认大小 的
[解决办法]
#include "Widget.hpp"
#include "ui_Widget.h"
#include <QPainter>
#include <QPrinter>
#include <QFile>
#include <QDebug>
#include <QRegExp>
#include <QTextDocument>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QDateTime>
#include <QPageSetupDialog>
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
ui->setupUi(this);
printer = new QPrinter();
printer->setFullPage(true);
printer->setOrientation(QPrinter::Portrait);
printer->setOutputFormat(QPrinter::NativeFormat);
connect(ui->printOrderButton, SIGNAL(clicked()), this, SLOT(printOrder()));
connect(ui->printOrdersButton, SIGNAL(clicked()), this, SLOT(printOrders()));
}
Widget::~Widget() {
delete ui;
}
void Widget::printOrder() {
printer->setPaperSize(QPrinter::A6);
// 没连打印机这个窗口弹不出来.
// QPageSetupDialog setupDialog(&printer, this);
// setupDialog.exec();
// qDebug() << printer.getPageMargins();
// 打印预览.
QPrintPreviewDialog preview(printer, this);
connect(&preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(printOrder(QPrinter*)));
preview.exec();
// printOrder(printer);
}
void Widget::printOrders() {
printer->setPaperSize(QPrinter::A4);
// 打印预览.
QPrintPreviewDialog preview(printer, this);
connect(&preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(printOrders(QPrinter*)));
preview.exec();
printOrders(printer);
}
void Widget::printOrder(QPrinter* printer) {
QString html = createOrderHtml();
printHtml(html, printer);
}
void Widget::printOrders(QPrinter* printer) {
QString html = createOrdersHtml();
printHtml(html, printer);
}
void Widget::printHtml(const QString &html, QPrinter *printer) {
QTextDocument doc;
QFont font;
font.setPointSize(10); // TODO: font size from settings.
doc.setDefaultFont(font);
doc.setHtml(html);
doc.print(printer);
ui->textEdit->setHtml(html);
}
QString Widget::createOrderHtml() {
QString html = readHtmlPattern("OrderHtmlPattern.html");
return html;
}
QString Widget::createOrdersHtml() {
QString html = readHtmlPattern("OrdersHtmlPattern.html");
QStringList lines = html.split("\n");
QString tr;
QString tr0;
QString tr1;
QString trs;
// Read the tr patterns.
for (int i = 0; i < lines.size(); i++) {
tr = lines.at(i).trimmed();
if (tr.contains("class="TR0"")) {
tr0 = tr.replace("<!--", "").replace("-->", "");
}
if (tr.contains("class="TR1"")) {
tr1 = tr.replace("<!--", "").replace("-->", "");
}
}
QStringList list;
list << "打印<br>内容<br>学习" << "打印<br>内容" << "打印" << "打印<br>内容<br>学习<br>去哪里学习";
qsrand(QDateTime::currentDateTime().toTime_t());
// Build trs using the orders.
for (int i = 0; i < 5; ++i) {
tr = (i & 0x1) == 0 ? tr0 : tr1;
trs += tr.arg("0001114449")
.arg("现金")
.arg(1500)
.arg(1350)
.arg("2013-03-13")
.arg("2013-03-12")
.arg(list.at(qrand() % 4));
}
// Put trs into the html's table.
html.replace("#trs", trs);
return html;
}
QString Widget::readHtmlPattern(const QString &path) const {
// Pattern file.
QFile htmlPatternFile(path);
if (!htmlPatternFile.open(QIODevice::ReadOnly)) {
qDebug() << QString("Can not find %1").arg(path);
return "";
}
QString html = htmlPatternFile.readAll();
// Close the files.
htmlPatternFile.close();
return html;
}