QWebEngine: print a page?

孤街醉人 提交于 2019-11-27 06:46:06

问题


The migration from QWebKit to QWebEngine seems to be much more complicated than Qt guys claimed. With QWebKit I could print a webpage easily via

QWebView->print(&printer);

With QWebEngine class QWebEngine view does not provide a method print(). Their browser example uses a class named QWebEngineFrame which offers a method print() - but the whole QWebEngineFrame is not defined anywhere!

So my question: how do I print a page using QWebEngine?


回答1:


I think the correct way to use QWebEngineView::render method because QWebEngineView is a QWidget. It accepts paint device as a first argument and you may pass QPrinter there for printing.

Update: If you can use the latest version of Qt, in Qt 5.8 a new function for printing page was added:

void QWebEnginePage::print(QPrinter *printer, FunctorOrLambda resultCallback);

Actually it first prints to temp PDF with QPrinter settings.

Here is the link to Qt docs.

You can read about this in our blog also.




回答2:


I would offer following code (for a while):

    QTextEdit *textEdit = new QTextEdit;
    ui.myWebView->page()->toHtml([textEdit](const QString &result){ textEdit->setHtml(result); });
    textEdit->print(somerinter);
    textEdit->deleteLater();



回答3:


Qt 5.7 includes print support in to pdf files for QWebEngine.

Use printToPdf function to export the current page in a pdf file. Example:

const QString fileName = QFileDialog::getSaveFileName(0,
                                                tr("Save pdf"),
                                                ".",
                                                tr("PDF Files (*.pdf)"));
if (fileName.isEmpty()) {
    return;
}
ui->webView->page()->printToPdf(fileName);



回答4:


QWebView->page()->print(&printer, [=](bool){});



来源:https://stackoverflow.com/questions/32711901/qwebengine-print-a-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!