2

在我的小应用程序中,我想预览每个页面中的一些 HTML 内容,所以我使用了以下代码

/*
* 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;
} 

我得到的结果页面

无法移动下一页

在多次测试结果页面后,我意识到打印机没有移动下一页来打印 HTML 内容。

我该如何解决这个问题?

谢谢!

4

1 回答 1

6

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

<div style=\"page-break-after:always\"></div>
于 2013-11-15T07:18:00.277 回答