Question

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

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!

Was it helpful?

Solution

I think you can try to add this to break the page.

<div style=\"page-break-after:always\"></div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top