+void BrowserView::print() const
+{
+ // Create a printer.
+ QPrinter printer;
+
+ // Set the resolution to be 300 dpi.
+ printer.setResolution(300);
+
+ // Create a printer dialog.
+ QPrintDialog printDialog(&printer, webEngineViewPointer);
+
+ // Display the dialog and print the page if instructed.
+ if (printDialog.exec() == QDialog::Accepted)
+ printWebpage(&printer);
+}
+
+void BrowserView::printPreview() const
+{
+ // Create a printer.
+ QPrinter printer;
+
+ // Set the resolution to be 300 dpi.
+ printer.setResolution(300);
+
+ // Create a print preview dialog.
+ QPrintPreviewDialog printPreviewDialog(&printer, webEngineViewPointer);
+
+ // Generate the print preview.
+ connect(&printPreviewDialog, SIGNAL(paintRequested(QPrinter *)), this, SLOT(printWebpage(QPrinter *)));
+
+ // Display the dialog.
+ printPreviewDialog.exec();
+}
+
+void BrowserView::printWebpage(QPrinter *printerPointer) const
+{
+ // Create an event loop. For some reason, the print preview doesn't produce any output unless it is run inside an event loop.
+ QEventLoop eventLoop;
+
+ // Print the webpage, converting the callback above into a `QWebEngineCallback<bool>`.
+ // Printing requires that the printer be a pointer, not a reference, or it will crash with much cursing.
+ webEnginePagePointer->print(printerPointer, [&eventLoop](bool printSuccess)
+ {
+ // Instruct the compiler to ignore the unused parameter.
+ (void) printSuccess;
+
+ // Quit the loop.
+ eventLoop.quit();
+ });
+
+ // Execute the loop.
+ eventLoop.exec();
+}
+