summaryrefslogtreecommitdiffstats
path: root/examples/webenginewidgets/demobrowser/browsermainwindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/webenginewidgets/demobrowser/browsermainwindow.cpp')
-rw-r--r--examples/webenginewidgets/demobrowser/browsermainwindow.cpp39
1 files changed, 38 insertions, 1 deletions
diff --git a/examples/webenginewidgets/demobrowser/browsermainwindow.cpp b/examples/webenginewidgets/demobrowser/browsermainwindow.cpp
index 4b53124da..e72a2b5fe 100644
--- a/examples/webenginewidgets/demobrowser/browsermainwindow.cpp
+++ b/examples/webenginewidgets/demobrowser/browsermainwindow.cpp
@@ -312,8 +312,12 @@ void BrowserMainWindow::setupMenu()
#if defined(QWEBENGINEPAGE_PRINT)
fileMenu->addAction(tr("P&rint Preview..."), this, SLOT(slotFilePrintPreview()));
fileMenu->addAction(tr("&Print..."), this, SLOT(slotFilePrint()), QKeySequence::Print);
- fileMenu->addSeparator();
#endif
+#ifndef QT_NO_PRINTER
+ fileMenu->addAction(tr("&Print to PDF..."), this, SLOT(slotFilePrintToPDF()));
+#endif // ifndef QT_NO_PRINTER
+ fileMenu->addSeparator();
+
QAction *action = fileMenu->addAction(tr("Private &Browsing..."), this, SLOT(slotPrivateBrowsing()));
action->setCheckable(true);
action->setChecked(BrowserApplication::instance()->privateBrowsing());
@@ -697,6 +701,39 @@ void BrowserMainWindow::slotFilePrint()
#endif
}
+void BrowserMainWindow::slotHandlePdfPrinted(const QByteArray& result)
+{
+ if (!result.size())
+ return;
+
+ QFile file(m_printerOutputFileName);
+
+ m_printerOutputFileName.clear();
+ if (!file.open(QFile::WriteOnly))
+ return;
+
+ file.write(result.data(), result.size());
+ file.close();
+}
+
+void BrowserMainWindow::slotFilePrintToPDF()
+{
+#ifndef QT_NO_PRINTER
+ if (!currentTab())
+ return;
+ QPrinter printer;
+ QPrintDialog *dialog = new QPrintDialog(&printer, this);
+ dialog->setWindowTitle(tr("Print Document"));
+ if (dialog->exec() != QDialog::Accepted || printer.outputFileName().isEmpty() || !m_printerOutputFileName.isEmpty())
+ return;
+
+ m_printerOutputFileName = printer.outputFileName();
+
+ currentTab()->page()->printToPdf(printer.pageLayout(), invoke(this, &BrowserMainWindow::slotHandlePdfPrinted));
+
+#endif // QT_NO_PRINTER
+}
+
#if defined(QWEBENGINEPAGE_PRINT)
void BrowserMainWindow::printRequested(QWebEngineFrame *frame)
{