void ReportPreview::on_printButton_clicked( )
{
prn.
setOutputFormat(QPrinter::PdfFormat);
prn.setOutputFileName("test.pdf");
prn.
setOrientation(QPrinter::Landscape);
if (prnDiag.
exec() == QDialog::Accepted) {
QTextDocument *t
= textBrowser
->document
()->clone
(0);
//Create a copy of the TextDocument
qreal w = (qreal)prn.width(); //Printer's page width
qreal h = (qreal)prn.height(); //Printer's page height
//Set the TextDocument's page size
t->documentLayout()->setPaintDevice(&prn);
p.begin(&prn); //Start printing
//Page 1
ctx.
clip = QRectF(0,
0,w,h
);
//Set the context clip to the first page t->documentLayout()->draw(&p,ctx);
//Page 2
prn.newPage();
ctx.
clip = QRectF(0,h
+1,w,h
);
//Set the context clip to the second page t->documentLayout()->draw(&p,ctx);
//Eventually there will be a FOR loop printing the pages specified in prnDiag
p.end(); //End printing
//Clean up
delete t;
}
}
void ReportPreview::on_printButton_clicked( )
{
QPrinter prn(QPrinter::HighResolution);
prn.setOutputFormat(QPrinter::PdfFormat);
prn.setOutputFileName("test.pdf");
prn.setPageSize(QPrinter::Letter);
prn.setColorMode(QPrinter::GrayScale);
prn.setOrientation(QPrinter::Landscape);
QPrintDialog prnDiag(&prn, this);
if (prnDiag.exec() == QDialog::Accepted)
{
QPainter p;
QAbstractTextDocumentLayout::PaintContext ctx;
QTextDocument *t = textBrowser->document()->clone(0); //Create a copy of the TextDocument
qreal w = (qreal)prn.width(); //Printer's page width
qreal h = (qreal)prn.height(); //Printer's page height
//Set the TextDocument's page size
t->documentLayout()->setPaintDevice(&prn);
t->setPageSize(QSizeF(w,h));
p.begin(&prn); //Start printing
//Page 1
ctx.clip = QRectF(0,0,w,h); //Set the context clip to the first page
t->documentLayout()->draw(&p,ctx);
//Page 2
prn.newPage();
ctx.clip = QRectF(0,h+1,w,h); //Set the context clip to the second page
t->documentLayout()->draw(&p,ctx);
//Eventually there will be a FOR loop printing the pages specified in prnDiag
p.end(); //End printing
//Clean up
delete t;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks