summaryrefslogtreecommitdiffstats
path: root/examples/webenginewidgets
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2019-04-30 10:39:42 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2019-04-30 10:40:26 +0200
commit4181db05fc8f1c96d0bbf2e88a64e6d3bb11c6cf (patch)
treef4f9127216f92102f3420949f82e3186fee15e54 /examples/webenginewidgets
parentb546cea1b5ba6721bf7fcb687c6c069d4442ba34 (diff)
parent26ac59af2306b4f6f83e791bb3e828b9f7b1a721 (diff)
Merge remote-tracking branch 'origin/5.12' into 5.13
Diffstat (limited to 'examples/webenginewidgets')
-rw-r--r--examples/webenginewidgets/printme/data/data.qrc7
-rw-r--r--examples/webenginewidgets/printme/data/icon.svg24
-rw-r--r--examples/webenginewidgets/printme/data/index.html24
-rw-r--r--examples/webenginewidgets/printme/data/style.css72
-rw-r--r--examples/webenginewidgets/printme/doc/images/printme-example.pngbin0 -> 42074 bytes
-rw-r--r--examples/webenginewidgets/printme/doc/src/printme.qdoc98
-rw-r--r--examples/webenginewidgets/printme/main.cpp76
-rw-r--r--examples/webenginewidgets/printme/printhandler.cpp115
-rw-r--r--examples/webenginewidgets/printme/printhandler.h79
-rw-r--r--examples/webenginewidgets/printme/printme.pro9
-rw-r--r--examples/webenginewidgets/simplebrowser/browser.cpp13
-rw-r--r--examples/webenginewidgets/simplebrowser/browser.h2
-rw-r--r--examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc23
-rw-r--r--examples/webenginewidgets/webenginewidgets.pro7
14 files changed, 533 insertions, 16 deletions
diff --git a/examples/webenginewidgets/printme/data/data.qrc b/examples/webenginewidgets/printme/data/data.qrc
new file mode 100644
index 000000000..a9c76cc7e
--- /dev/null
+++ b/examples/webenginewidgets/printme/data/data.qrc
@@ -0,0 +1,7 @@
+<RCC>
+ <qresource prefix="/">
+ <file>index.html</file>
+ <file>style.css</file>
+ <file>icon.svg</file>
+ </qresource>
+</RCC>
diff --git a/examples/webenginewidgets/printme/data/icon.svg b/examples/webenginewidgets/printme/data/icon.svg
new file mode 100644
index 000000000..b90ff26dd
--- /dev/null
+++ b/examples/webenginewidgets/printme/data/icon.svg
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 19.2.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
+<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+ width="94px" height="94px" viewBox="0 0 94 94" enable-background="new 0 0 94 94" xml:space="preserve">
+<g>
+ <circle fill="none" cx="47" cy="47" r="47"/>
+ <g>
+ <path fill="#46A2DA" d="M47,92.979c-11.779,0-23.559-4.484-32.526-13.451C-3.461,61.591-3.461,32.409,14.472,14.474
+ C32.41-3.463,61.592-3.461,79.526,14.473c17.935,17.936,17.935,47.119,0.002,65.054l-0.002,0.001
+ C70.559,88.495,58.779,92.979,47,92.979z"/>
+ </g>
+ <path fill="#80C342" d="M93,47C93,21.595,72.405,1,47,1C34.297,1,22.797,6.149,14.473,14.473l65.054,65.054
+ C87.851,71.203,93,59.703,93,47z"/>
+ <g>
+ <path fill="#46A2DA" d="M47,65c-4.808,0-9.328-1.873-12.728-5.272c-7.018-7.019-7.018-18.438,0-25.456
+ C37.672,30.873,42.192,29,47,29s9.328,1.873,12.728,5.272c7.018,7.019,7.018,18.438,0,25.456C56.328,63.127,51.808,65,47,65z"/>
+ <path fill="#FFFFFF" d="M62.248,59.919c6.671-7.858,6.312-19.644-1.105-27.061C57.237,28.953,52.118,27,47,27
+ c-5.118,0-10.237,1.953-14.142,5.858c-7.81,7.81-7.81,20.474,0,28.284C36.763,65.047,41.882,67,47,67
+ c4.379,0,8.752-1.441,12.372-4.3L77.88,81.209c0.989-0.895,1.935-1.837,2.843-2.814L62.248,59.919z M35.686,58.314
+ c-6.238-6.238-6.238-16.389,0-22.627C38.708,32.664,42.726,31,47,31c4.274,0,8.292,1.664,11.314,4.686
+ c6.238,6.238,6.238,16.389,0,22.627C55.292,61.336,51.274,63,47,63C42.726,63,38.708,61.336,35.686,58.314z"/>
+ </g>
+</g>
+</svg>
diff --git a/examples/webenginewidgets/printme/data/index.html b/examples/webenginewidgets/printme/data/index.html
new file mode 100644
index 000000000..cf286e85a
--- /dev/null
+++ b/examples/webenginewidgets/printme/data/index.html
@@ -0,0 +1,24 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>PrintMe</title>
+ <link rel="stylesheet" type="text/css" href="style.css">
+ <script>
+ function printNow() {
+ window.print();
+ }
+ </script>
+ </head>
+ <body>
+ <form class="form">
+ <img class="logo" src="icon.svg" alt="qtwebengine">
+ <div class="header">
+ <h1>Hello Paper World!</h1>
+ <h2>Press Ctrl+p to print with print preview</h2>
+ <h2>Press Ctrl+Shift+p to print without print preview</h2>
+ <h2>Click the button to print using JavaScript</h2>
+ <p class="button" onclick="printNow()">Print Now</p>
+ </form>
+ </body>
+</html>
diff --git a/examples/webenginewidgets/printme/data/style.css b/examples/webenginewidgets/printme/data/style.css
new file mode 100644
index 000000000..cf6a2b7bf
--- /dev/null
+++ b/examples/webenginewidgets/printme/data/style.css
@@ -0,0 +1,72 @@
+html,body {
+ height:100%;
+ width:100%;
+ margin:0;
+}
+ body {
+ display:flex;
+}
+ .logo {
+ width: 75px;
+ height: 75px;
+ float: left;
+ margin: 20px 20px 0px 20px;
+ -webkit-animation:spin 8s linear infinite;
+}
+ @-webkit-keyframes spin {
+ 100% {
+ -webkit-transform: rotate(360deg);
+ }
+}
+ .header {
+ display: inline
+}
+ .form {
+ width: 480px;
+ height: 170px;
+ background: -webkit-linear-gradient(bottom, #ddd, #fff);
+ border: 1px solid #999;
+ border-radius: 12px;
+ color: #46a;
+ font-family: 'Lucida Sans Unicode', 'Lucida Grande', sans-serif;
+ font-size: 14px;
+ font-style: italic;
+ font-weight: bold;
+ margin: auto;
+ padding: 10px;
+ position: relative;
+ line-height: 26px;
+ text-decoration: none;
+ -webkit-box-shadow: 0px 0px 5px #444;
+}
+ h1 {
+ padding-left:40px;
+ color: #46a2da;
+}
+ h2 {
+ color: #80c342;
+ font-size: 13px;
+ margin-top: -20px;
+}
+ span {
+ margin-left: 20px;
+}
+ .button{
+ display: inline-block;
+ background: #46a2da;
+ width: 100px;
+ height: 30px;
+ padding: 0px;
+ text-align: center;
+ font-weight: bold;
+ color: #ffffff;
+ text-decoration: none;
+ border: 1px solid #999;
+ margin-left: 190px;
+}
+ .button:hover {
+ background-color: #46a200
+}
+ .button:active {
+ background-color: #3e8e41;
+}
diff --git a/examples/webenginewidgets/printme/doc/images/printme-example.png b/examples/webenginewidgets/printme/doc/images/printme-example.png
new file mode 100644
index 000000000..a636972fd
--- /dev/null
+++ b/examples/webenginewidgets/printme/doc/images/printme-example.png
Binary files differ
diff --git a/examples/webenginewidgets/printme/doc/src/printme.qdoc b/examples/webenginewidgets/printme/doc/src/printme.qdoc
new file mode 100644
index 000000000..d05d5147b
--- /dev/null
+++ b/examples/webenginewidgets/printme/doc/src/printme.qdoc
@@ -0,0 +1,98 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:FDL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Free Documentation License Usage
+** Alternatively, this file may be used under the terms of the GNU Free
+** Documentation License version 1.3 as published by the Free Software
+** Foundation and appearing in the file included in the packaging of
+** this file. Please review the following information to ensure
+** the GNU Free Documentation License version 1.3 requirements
+** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+/*!
+ \example webenginewidgets/printme
+ \title WebEngine Widgets PrintMe Example
+ \ingroup webengine-widgetexamples
+ \brief Demonstrates how to print web pages using Qt WebEngine Widgets.
+
+ \image printme-example.png
+
+ \e PrintMe demonstrates how to use the \l{QWebEnginePage} and
+ \l{QPrintDialog} classes to print a web page. Further, it shows how
+ to implement print preview by using the \l{QPrintPreviewDialog} class.
+ For completeness, it also illustrates how to trigger a printing request
+ within JavaScript.
+
+ \include examples-run.qdocinc
+
+ \section1 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 \c{onclick} event attribute that calls
+ the JavaScript \c{window.print()} function.
+
+ \quotefromfile webenginewidgets/printme/data/index.html
+ \skipto <html
+ \printuntil html>
+
+ \section1 Main Function
+
+ In the \c main function, we first instantiate a QWebEngineView and set the
+ URL to our internal HTML page. Next, we create a \c 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.
+
+ \quotefromfile webenginewidgets/printme/main.cpp
+ \skipto QWebEngineView view
+ \printto return
+
+ \section1 Print Handler
+
+ In the \c{PrintHandler} function, we first implement \c{printPreview()},
+ where we instantiate \l{QPrinter} together with \l{QPrintPreviewDialog}.
+ We need the \l{QPrintPreviewDialog::paintRequested} handle to generate a
+ set of preview pages.
+
+ \quotefromfile webenginewidgets/printme/printhandler.cpp
+ \skipto PrintHandler::printPreview(
+ \printuntil /^\}/
+
+ Now we can implement the \c{PrintHandler::printDocument()} slot, which is
+ called in response to the \l{QPrintPreviewDialog::paintRequested} signal.
+
+ \quotefromfile webenginewidgets/printme/printhandler.cpp
+ \skipto PrintHandler::printDocument(
+ \printuntil /^\}/
+
+ To do actual painting on a printer, we call the \l{QWebEnginePage::print()}
+ function. Because this call blocks the main event loop, we need to create
+ a local one. We begin the local event loop by calling
+ \l{QEventLoop::exec()}. When the local event loop terminates,
+ we check for \c{result} and report any errors that occurred.
+
+ The last function we implement, \c{PrintHandler::print()}, is trivial,
+ because it simply opens \l{QPrintDialog} and calls the previously
+ implemented \c{PrintHandler::printDocument()}.
+
+ \quotefromfile webenginewidgets/printme/printhandler.cpp
+ \skipto PrintHandler::print(
+ \printuntil /^\}/
+*/
diff --git a/examples/webenginewidgets/printme/main.cpp b/examples/webenginewidgets/printme/main.cpp
new file mode 100644
index 000000000..e4d6d9dc8
--- /dev/null
+++ b/examples/webenginewidgets/printme/main.cpp
@@ -0,0 +1,76 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "printhandler.h"
+#include <QApplication>
+#include <QShortcut>
+#include <QWebEngineView>
+
+int main(int argc, char *argv[])
+{
+ QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
+ QApplication app(argc, argv);
+
+ QWebEngineView view;
+ view.setUrl(QUrl(QStringLiteral("qrc:/index.html")));
+ view.resize(1024, 750);
+ view.show();
+
+ PrintHandler handler;
+ handler.setPage(view.page());
+
+ auto printPreviewShortCut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_P), &view);
+ auto printShortCut = new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_P), &view);
+
+ QObject::connect(printPreviewShortCut, &QShortcut::activated, &handler, &PrintHandler::printPreview);
+ QObject::connect(printShortCut, &QShortcut::activated, &handler, &PrintHandler::print);
+
+ return app.exec();
+}
diff --git a/examples/webenginewidgets/printme/printhandler.cpp b/examples/webenginewidgets/printme/printhandler.cpp
new file mode 100644
index 000000000..d8c4fc177
--- /dev/null
+++ b/examples/webenginewidgets/printme/printhandler.cpp
@@ -0,0 +1,115 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "printhandler.h"
+#include <QEventLoop>
+#include <QPrintDialog>
+#include <QPrinter>
+#include <QPainter>
+#include <QPrintPreviewDialog>
+#include <QWebEnginePage>
+
+PrintHandler::PrintHandler(QObject *parent)
+ : QObject(parent)
+{
+
+}
+
+void PrintHandler::setPage(QWebEnginePage *page)
+{
+ Q_ASSERT(!m_page);
+ m_page = page;
+ connect(m_page, &QWebEnginePage::printRequested, this, &PrintHandler::printPreview);
+}
+
+void PrintHandler::print()
+{
+ QPrinter printer(QPrinter::HighResolution);
+ QPrintDialog dialog(&printer, m_page->view());
+ if (dialog.exec() != QDialog::Accepted)
+ return;
+ printDocument(&printer);
+}
+
+void PrintHandler::printDocument(QPrinter *printer)
+{
+ QEventLoop loop;
+ bool result;
+ auto printPreview = [&](bool success) { result = success; loop.quit(); };
+ m_page->print(printer, std::move(printPreview));
+ loop.exec();
+ if (!result) {
+ QPainter painter;
+ if (painter.begin(printer)) {
+ QFont font = painter.font();
+ font.setPixelSize(20);
+ painter.setFont(font);
+ painter.drawText(QPointF(10,25),
+ QStringLiteral("Could not generate print preview."));
+
+ painter.end();
+ }
+ }
+}
+
+void PrintHandler::printPreview()
+{
+ if (!m_page)
+ return;
+ if (m_inPrintPreview)
+ return;
+ m_inPrintPreview = true;
+ QPrinter printer;
+ QPrintPreviewDialog preview(&printer, m_page->view());
+ connect(&preview, &QPrintPreviewDialog::paintRequested,
+ this, &PrintHandler::printDocument);
+ preview.exec();
+ m_inPrintPreview = false;
+}
diff --git a/examples/webenginewidgets/printme/printhandler.h b/examples/webenginewidgets/printme/printhandler.h
new file mode 100644
index 000000000..69b71f4a8
--- /dev/null
+++ b/examples/webenginewidgets/printme/printhandler.h
@@ -0,0 +1,79 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef PRINTHANDLER_H
+#define PRINTHANDLER_H
+
+#include <QObject>
+
+QT_BEGIN_NAMESPACE
+class QPainter;
+class QPrinter;
+class QWebEnginePage;
+QT_END_NAMESPACE
+
+class PrintHandler : public QObject
+{
+ Q_OBJECT
+public:
+ PrintHandler(QObject *parent = nullptr);
+ void setPage(QWebEnginePage *page);
+
+public slots:
+ void print();
+ void printPreview();
+ void printDocument(QPrinter *printer);
+
+private:
+ QWebEnginePage *m_page = nullptr;
+ bool m_inPrintPreview = false;
+};
+
+#endif // PRINTHANDLER_H
diff --git a/examples/webenginewidgets/printme/printme.pro b/examples/webenginewidgets/printme/printme.pro
new file mode 100644
index 000000000..15b3959d5
--- /dev/null
+++ b/examples/webenginewidgets/printme/printme.pro
@@ -0,0 +1,9 @@
+QT += webenginewidgets printsupport
+
+HEADERS = printhandler.h
+SOURCES = main.cpp \
+ printhandler.cpp
+RESOURCES = data/data.qrc
+
+target.path = $$[QT_INSTALL_EXAMPLES]/webenginewidgets/printme
+INSTALLS += target
diff --git a/examples/webenginewidgets/simplebrowser/browser.cpp b/examples/webenginewidgets/simplebrowser/browser.cpp
index 5c6dbd35e..68458b2a4 100644
--- a/examples/webenginewidgets/simplebrowser/browser.cpp
+++ b/examples/webenginewidgets/simplebrowser/browser.cpp
@@ -51,8 +51,6 @@
#include "browser.h"
#include "browserwindow.h"
-#include <QWebEngineProfile>
-
Browser::Browser()
{
// Quit application if the download manager window is the only remaining window
@@ -61,14 +59,17 @@ Browser::Browser()
QObject::connect(
QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested,
&m_downloadManagerWidget, &DownloadManagerWidget::downloadRequested);
- QObject::connect(
- &m_otrProfile, &QWebEngineProfile::downloadRequested,
- &m_downloadManagerWidget, &DownloadManagerWidget::downloadRequested);
}
BrowserWindow *Browser::createWindow(bool offTheRecord)
{
- auto profile = offTheRecord ? &m_otrProfile : QWebEngineProfile::defaultProfile();
+ if (offTheRecord && !m_otrProfile) {
+ m_otrProfile.reset(new QWebEngineProfile);
+ QObject::connect(
+ m_otrProfile.get(), &QWebEngineProfile::downloadRequested,
+ &m_downloadManagerWidget, &DownloadManagerWidget::downloadRequested);
+ }
+ auto profile = offTheRecord ? m_otrProfile.get() : QWebEngineProfile::defaultProfile();
auto mainWindow = new BrowserWindow(this, profile, false);
m_windows.append(mainWindow);
QObject::connect(mainWindow, &QObject::destroyed, [this, mainWindow]() {
diff --git a/examples/webenginewidgets/simplebrowser/browser.h b/examples/webenginewidgets/simplebrowser/browser.h
index fbc8465d2..4c17121df 100644
--- a/examples/webenginewidgets/simplebrowser/browser.h
+++ b/examples/webenginewidgets/simplebrowser/browser.h
@@ -73,6 +73,6 @@ public:
private:
QVector<BrowserWindow*> m_windows;
DownloadManagerWidget m_downloadManagerWidget;
- QWebEngineProfile m_otrProfile;
+ QScopedPointer<QWebEngineProfile> m_otrProfile;
};
#endif // BROWSER_H
diff --git a/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc b/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc
index b5ad13626..251ca5ad8 100644
--- a/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc
+++ b/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc
@@ -270,7 +270,7 @@
Implementing private browsing is quite easy using \QWE. All one has
to do is to create a new \l{QWebEngineProfile} and use it in the
\l{QWebEnginePage} instead of the default profile. In the example this new
- profile is created and owned by the \c Browser object:
+ profile is owned by the \c Browser object:
\quotefromfile webenginewidgets/simplebrowser/browser.h
\skipto /^class Browser$/
@@ -285,15 +285,24 @@
\printline m_otrProfile
\printline /^\};$/
- The default constructor for \l{QWebEngineProfile} already puts it in
- \e{off-the-record} mode. All that is left to do is to pass the appropriate
- profile down to the appropriate \l QWebEnginePage objects. The \c Browser
- object will hand to each new \c BrowserWindow either the global default
- profile (see \l{QWebEngineProfile::defaultProfile}) or its own
- off-the-record profile:
+ Required profile for \e{private browsing} is created together with its
+ first window. The default constructor for \l{QWebEngineProfile} already
+ puts it in \e{off-the-record} mode.
\quotefromfile webenginewidgets/simplebrowser/browser.cpp
+
\skipto Browser::createWindow
+ \printuntil m_otrProfile.reset
+ \dots
+
+ All that is left to do is to pass the appropriate profile down to the
+ appropriate \l QWebEnginePage objects. The \c Browser object will hand to
+ each new \c BrowserWindow either the global default profile
+ (see \l{QWebEngineProfile::defaultProfile}) or one shared
+ \e{off-the-record} profile instance:
+
+ \dots
+ \skipto m_otrProfile.get
\printuntil mainWindow = new BrowserWindow
\skipto return mainWindow
\printuntil /^\}/
diff --git a/examples/webenginewidgets/webenginewidgets.pro b/examples/webenginewidgets/webenginewidgets.pro
index 20c7ead50..0d47aac80 100644
--- a/examples/webenginewidgets/webenginewidgets.pro
+++ b/examples/webenginewidgets/webenginewidgets.pro
@@ -1,5 +1,5 @@
include($$QTWEBENGINE_OUT_ROOT/src/core/qtwebenginecore-config.pri) # workaround for QTBUG-68093
-QT_FOR_CONFIG += webenginecore
+QT_FOR_CONFIG += webenginecore webenginecore-private
TEMPLATE=subdirs
@@ -7,7 +7,6 @@ SUBDIRS += \
minimal \
contentmanipulation \
cookiebrowser \
- html2pdf \
simplebrowser \
stylesheetbrowser \
videoplayer \
@@ -16,6 +15,10 @@ SUBDIRS += \
qtConfig(webengine-geolocation): SUBDIRS += maps
qtConfig(webengine-webchannel): SUBDIRS += markdowneditor
+qtConfig(webengine-printing-and-pdf) {
+ SUBDIRS += printme html2pdf
+}
+
qtConfig(webengine-spellchecker):!qtConfig(webengine-native-spellchecker):!cross_compile {
SUBDIRS += spellchecker
} else {