WebEngine Widgets PrintMe Example▲
PrintMe demonstrates how to use the QWebEnginePage and QPrintDialog classes to print a web page. Further, it shows how to implement print preview by using the QPrintPreviewDialog class. For completeness, it also illustrates how to trigger a printing request within JavaScript.
Running the Example▲
To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.
Simple HTML Page▲
In this example, we create an internal HTML page that is added as a resource collection file (.qrc). The page shows only a small HTML message box that explains how to trigger printing by using keyboard shortcuts or clicking a button. The button has the JavaScript onclick event attribute that calls the JavaScript window.print() function.
&
lt;html lang=
"en"
&
gt;
&
lt;head&
gt;
&
lt;meta charset=
"utf-8"
&
gt;
&
lt;title&
gt;PrintMe&
lt;/
title&
gt;
&
lt;link rel=
"stylesheet"
type=
"text/css"
href=
"style.css"
&
gt;
&
lt;script&
gt;
function printNow() {
window.print();
}
&
lt;/
script&
gt;
&
lt;/
head&
gt;
&
lt;body&
gt;
&
lt;form class
=
"form"
&
gt;
&
lt;img class
=
"logo"
src=
"icon.svg"
alt=
"qtwebengine"
&
gt;
&
lt;div class
=
"header"
&
gt;
&
lt;h1&
gt;Hello Paper World!&
lt;/
h1&
gt;
&
lt;h2&
gt;Press Ctrl+
p to print with print preview&
lt;/
h2&
gt;
&
lt;h2&
gt;Press Ctrl+
Shift+
p to print without print preview&
lt;/
h2&
gt;
&
lt;h2&
gt;Click the button to print using
JavaScript&
lt;/
h2&
gt;
&
lt;p class
=
"button"
onclick=
"printNow()"
&
gt;Print Now&
lt;/
p&
gt;
&
lt;/
form&
gt;
&
lt;/
body&
gt;
&
lt;/
html&
gt;
Main Function▲
In the main function, we first instantiate a QWebEngineView and set the URL to our internal HTML page. Next, we create a PrintHandler instance and pass the requested page. For convenience, we also create keyboard shortcuts that can be used to call a print dialog or print preview dialog.
QWebEngineView view;
view.setUrl(QUrl(QStringLiteral("qrc:/index.html"
)));
view.resize(1024
, 750
);
view.show();
PrintHandler handler;
handler.setView(&
amp;view);
auto
printPreviewShortCut =
new
QShortcut(QKeySequence(Qt::
CTRL |
Qt::
Key_P), &
amp;view);
auto
printShortCut =
new
QShortcut(QKeySequence(Qt::
CTRL |
Qt::
SHIFT |
Qt::
Key_P), &
amp;view);
QObject::
connect(printPreviewShortCut, &
amp;QShortcut::
activated, &
amp;handler, &
amp;PrintHandler::
printPreview);
QObject::
connect(printShortCut, &
amp;QShortcut::
activated, &
amp;handler, &
amp;PrintHandler::
print);
Print Handler▲
In the PrintHandler class, we first implement printPreview(), where we instantiate QPrintPreviewDialog. We need the QPrintPreviewDialog::paintRequested handle to generate a set of preview pages.
void
PrintHandler::
printPreview()
{
if
(!
m_view)
return
;
if
(m_inPrintPreview)
return
;
m_inPrintPreview =
true
;
QPrintPreviewDialog preview(&
amp;m_printer, m_view);
connect(&
amp;preview, &
amp;QPrintPreviewDialog::
paintRequested,
this
, &
amp;PrintHandler::
printDocument);
preview.exec();
m_inPrintPreview =
false
;
}
Now we can implement the PrintHandler::printDocument() slot, which is called in response to the QPrintPreviewDialog::paintRequested signal. To do actual painting on a printer, we call the QWebEngineView::print() function. Because this call is asynchronous, we need to use a local event loop. We begin the local event loop by calling QEventLoop::exec().
void
PrintHandler::
printDocument(QPrinter *
printer)
{
m_view-&
gt;print(printer);
m_waitForResult.exec();
}
To get notified about the result of printing job, we implement PrintHandler::printFinished() slot as handler of QWebEngineView::printFinished() signal. We check for success and report any errors that occurred.
void
PrintHandler::
printFinished(bool
success)
{
if
(!
success) {
QPainter painter;
if
(painter.begin(&
amp;m_printer)) {
QFont font =
painter.font();
font.setPixelSize(20
);
painter.setFont(font);
painter.drawText(QPointF(10
,25
),
QStringLiteral("Could not generate print preview."
));
painter.end();
}
}
m_waitForResult.quit();
}
The last function we implement, PrintHandler::print(), is trivial, because it simply opens QPrintDialog and calls the previously implemented PrintHandler::printDocument().
void
PrintHandler::
print()
{
QPrintDialog dialog(&
amp;m_printer, m_view);
if
(dialog.exec() !=
QDialog::
Accepted)
return
;
printDocument(&
amp;m_printer);
}