Cannot move to next page to print HTML content with QPrinter
-
In my small application, I want to preview some HTML content in each page so I used the following code
@
/*-
Handle events when clicking button to preview content
*/
void MainWindow::on_pushButton_clicked()
{
QPrinter printer;
printer.setPaperSize(QPrinter::A4);
printer.setOrientation(QPrinter::Portrait);
printer.setFullPage(true);QPrintPreviewDialog printPreview = new QPrintPreviewDialog(&printer);
connect(printPreview, SIGNAL(paintRequested(QPrinter)), this, SLOT(printAllTitle(QPrinter*)));printPreview->setWindowTitle("Demo");
Qt::WindowFlags flags(Qt::WindowTitleHint);
printPreview->setWindowFlags(flags);
printPreview->showMaximized();
printPreview->exec();
}
/*
-
Show preview content
*/
void MainWindow::printAllTitle(QPrinter *printer)
{
QVector<QString> titles;
titles.push_back("Title 1");
titles.push_back("Title 2");QString strStream;
QTextStream out(&strStream);for (int i = 0; i < titles.size(); i++) {
out << "<html><head></head><body>";
out << "<p style="font-size:20pt">" + titles.at(i) + "</p>";
out << "</body></html>";printer->newPage(); // Don't move the next page !!!
}
QTextDocument *document = new QTextDocument();
document->setHtml(strStream);
document->print(printer);
delete document;
}
@
And the result page I got
!http://i.stack.imgur.com/5qATo.png(Cannot move the next page)!
After testing the result page many times, I realized that the Printer didn't move the next page to print HTML content.
How can I solve this issue?
Thanks!
-
-
@Calling newPage() on an inactive QPrinter object will always fail. @
So, may be you should check the PrinterState before you call newPage.