From 2609e257926c36e211a5aec9d6675e27dd447aad Mon Sep 17 00:00:00 2001 From: Michael Bruning Date: Wed, 10 Aug 2016 17:26:18 +0200 Subject: Add printing on a QPrinter to the demobrowser Change-Id: I307103956bf50469a5c4cbe3656f655925f55653 Reviewed-by: Allan Sandfeld Jensen --- .../demobrowser/browsermainwindow.cpp | 32 ++++++++++++++++------ .../demobrowser/browsermainwindow.h | 14 ++++++++-- 2 files changed, 35 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/webenginewidgets/demobrowser/browsermainwindow.cpp b/examples/webenginewidgets/demobrowser/browsermainwindow.cpp index 246ab2c0c..09f9a746b 100644 --- a/examples/webenginewidgets/demobrowser/browsermainwindow.cpp +++ b/examples/webenginewidgets/demobrowser/browsermainwindow.cpp @@ -109,6 +109,9 @@ BrowserMainWindow::BrowserMainWindow(QWidget *parent, Qt::WindowFlags flags) , m_historyForward(0) , m_stop(0) , m_reload(0) +#ifndef QT_NO_PRINTER + , m_currentPrinter(nullptr) +#endif { setToolButtonStyle(Qt::ToolButtonFollowStyle); setAttribute(Qt::WA_DeleteOnClose, true); @@ -312,6 +315,8 @@ void BrowserMainWindow::setupMenu() fileMenu->addSeparator(); #if defined(QWEBENGINEPAGE_PRINT) fileMenu->addAction(tr("P&rint Preview..."), this, SLOT(slotFilePrintPreview())); +#endif +#ifndef QT_NO_PRINTER fileMenu->addAction(tr("&Print..."), this, SLOT(slotFilePrint()), QKeySequence::Print); #endif fileMenu->addAction(tr("&Print to PDF..."), this, SLOT(slotFilePrintToPDF())); @@ -713,10 +718,10 @@ void BrowserMainWindow::slotFilePrintPreview() void BrowserMainWindow::slotFilePrint() { -#if defined(QWEBENGINEPAGE_PRINT) +#ifndef QT_NO_PRINTER if (!currentTab()) return; - printRequested(currentTab()->page()->mainFrame()); + printRequested(currentTab()->page()); #endif } @@ -750,16 +755,27 @@ void BrowserMainWindow::slotFilePrintToPDF() currentTab()->page()->printToPdf(invoke(this, &BrowserMainWindow::slotHandlePdfPrinted), dialog->pageLayout()); } -#if defined(QWEBENGINEPAGE_PRINT) -void BrowserMainWindow::printRequested(QWebEngineFrame *frame) +#ifndef QT_NO_PRINTER +void BrowserMainWindow::slotHandlePagePrinted(bool result) +{ + delete m_currentPrinter; + m_currentPrinter = nullptr; +} + + +void BrowserMainWindow::printRequested(QWebEnginePage *page) { #ifndef QT_NO_PRINTDIALOG - QPrinter printer; - QPrintDialog *dialog = new QPrintDialog(&printer, this); + if (m_currentPrinter) + return; + m_currentPrinter = new QPrinter(); + QPrintDialog *dialog = new QPrintDialog(m_currentPrinter, this); dialog->setWindowTitle(tr("Print Document")); - if (dialog->exec() != QDialog::Accepted) + if (dialog->exec() != QDialog::Accepted) { + slotHandlePagePrinted(false); return; - frame->print(&printer); + } + page->print(m_currentPrinter, invoke(this, &BrowserMainWindow::slotHandlePagePrinted)); #endif } #endif diff --git a/examples/webenginewidgets/demobrowser/browsermainwindow.h b/examples/webenginewidgets/demobrowser/browsermainwindow.h index 7bd1ffaf1..91e1c1d2f 100644 --- a/examples/webenginewidgets/demobrowser/browsermainwindow.h +++ b/examples/webenginewidgets/demobrowser/browsermainwindow.h @@ -56,7 +56,10 @@ #include QT_BEGIN_NAMESPACE -class QWebEngineFrame; +#ifndef QT_NO_PRINTER +class QPrinter; +#endif +class QWebEnginePage; QT_END_NAMESPACE class AutoSaver; @@ -139,8 +142,9 @@ private slots: void slotSwapFocus(); void slotHandlePdfPrinted(const QByteArray&); -#if defined(QWEBENGINEPAGE_PRINT) - void printRequested(QWebEngineFrame *frame); +#ifndef QT_NO_PRINTER + void slotHandlePagePrinted(bool result); + void printRequested(QWebEnginePage *page); #endif void geometryChangeRequested(const QRect &geometry); void updateToolbarActionText(bool visible); @@ -176,6 +180,10 @@ private: QAction *m_restoreLastSession; QAction *m_addBookmark; +#ifndef QT_NO_PRINTER + QPrinter *m_currentPrinter; +#endif + QIcon m_reloadIcon; QIcon m_stopIcon; -- cgit v1.2.3