summaryrefslogtreecommitdiffstats
path: root/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc
blob: 2ca4bfc457195cb63baf1d90760883acca67a37d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: http://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 http://www.qt.io/terms-conditions. For further
** information use the contact form at http://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: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example webenginewidgets/simplebrowser
    \title WebEngine Widgets Simple Browser Example
    \ingroup webengine-widgetexamples
    \brief A simple browser based on Qt WebEngine Widgets

    \image simplebrowser.png

    \e {Simple Browser} demonstrates how to use the
    \l{Qt WebEngine Widgets C++ Classes}{Qt WebEngine C++ classes} to develop a
    small Web browser application that contains the following elements:

    \list
        \li Menu bar for opening stored pages and managing windows and tabs.
        \li Navigation bar for entering a URL and for moving backward and
            forward in the web page browsing history.
        \li Multi-tab area for displaying web content within tabs.
        \li Status bar for displaying hovered links.
        \li A simple download manager.
    \endlist

    The web content can be opened in new tabs or separate windows. HTTP and
    proxy authentication can be used for accessing web pages.

    \include examples-run.qdocinc

    \section1 Class Hierarchy

    We start with sketching a diagram of the main classes that we are going to
    implement:

    \image simplebrowser-model.png

    \list
        \li \c{Browser} is a class managing the application windows.
        \li \c{BrowserWindow} is a \l QMainWindow showing the menu, a navigation
            bar, \c {TabWidget}, and a status bar.
        \li \c{TabWidget} is a \l QTabWidget and contains one or multiple
            browser tabs.
        \li \c{WebView} is a \l QWebEngineView, provides a view for \c{WebPage},
            and is added as a tab in \c{TabWidget}.
        \li \c{WebPage} is a \l QWebEnginePage that represents website content.
    \endlist

    Additionally, we will implement some auxiliary classes:

    \list
        \li \c{WebPopupWindow} is a \l QWidget for showing popup windows.
        \li \c{DownloadManagerWidget} is a \l QWidget implementing the downloads
        list.
    \endlist

    \section1 Creating the Browser Main Window

    This example supports multiple main windows that are owned by a \c Browser
    object. This class also owns the \c DownloadManagerWidget and could be used
    for further functionality, such as bookmarks and history managers.

    In \c main.cpp, we create the first \c BrowserWindow instance and add it
    to the \c Browser object. If no arguments are passed on the command line,
    we open the \l{Qt Homepage}:

    \quotefromfile webenginewidgets/simplebrowser/main.cpp
    \skipto main
    \printuntil }

    \section1 Creating Tabs

    The \c BrowserWindow constructor initializes all the necessary user interface
    related objects. The \c centralWidget of \c BrowserWindow contains an instance of
    \c TabWidget. The \c TabWidget contains one or several \c WebView instances as tabs,
    and delegates it's signals and slots to the currently selected one:

    \quotefromfile webenginewidgets/simplebrowser/tabwidget.h
    \skipto TabWidget :
    \printuntil {
    \dots
    \skipto signals
    \printuntil triggerWebPageAction
    \skipto }
    \dots
    \printline };

    Each tab contains an instance of \c WebView:

    \quotefromfile webenginewidgets/simplebrowser/tabwidget.cpp
    \skipto TabWidget::createTab(
    \printuntil }
    \skipto TabWidget::createBackgroundTab(
    \printuntil }

    In \c TabWidget::setupView(), we make sure that the \c TabWidget always forwards
    the signals of the currently selected \c WebView:

    \quotefromfile webenginewidgets/simplebrowser/tabwidget.cpp
    \skipto TabWidget::setupView
    \printuntil emit loadProgress
    \skipto closeTab
    \skipto });
    \printline }
    \dots
    \printline }

    \section1 Implementing WebView Functionality

    The \c WebView is derived from QWebEngineView to support the following
    functionality:

    \list
        \li Displaying error messages in case \c renderProcess dies
        \li Handling \c createWindow requests
        \li Adding custom menu items to context menus
    \endlist

    First, we create the WebView with the necessary methods and signals:

    \quotefromfile webenginewidgets/simplebrowser/webview.h
    \skipto WebView :
    \printuntil WebView(
    \dots
    \skipto protected:
    \printuntil webActionEnabledChanged
    \skipto }
    \dots
    \printline };

    \section2 Displaying Error Messages

    If the render process is terminated, we display a QMessageBox with an error
    code, and then we reload the page:

    \quotefromfile webenginewidgets/simplebrowser/webview.cpp
    \skipto WebView::WebView(QWidget *parent)
    \printuntil {
    \skipto renderProcessTerminated
    \dots
    \printuntil QTimer
    \printline });
    \printline }

    \section2 Managing WebWindows

    The loaded page might want to create windows of the type
    QWebEnginePage::WebWindowType, for example, when a JavaScript program
    requests to open a document in a new window or dialog.
    This is handled by overriding \c QWebView::createWindow():

    \skipto WebView::createWindow(
    \printuntil return nullptr;
    \printuntil }

    In case of \c QWebEnginePage::WebDialog, we create an instance of a custom \c WebPopupWindow class:

    \quotefromfile webenginewidgets/simplebrowser/webpopupwindow.h
    \skipto class WebPopupWindow
    \printuntil };

    \section2 Adding Context Menu Items

    We add menu items to the context menu, so that users can right-click a link
    to have it opened in the same tab, a new window, or a new tab. We override
    QWebEngineView::contextMenuEvent and use
    QWebEnginePage::createStandardContextMenu to create a default QMenu with a
    default list of QWebEnginePage::WebAction actions.

    The default name for QWebEnginePage::OpenLinkInThisWindow action is
    \uicontrol Follow. For clarity, we rename it
    \uicontrol {Open Link in This Tab}. Also, we add the actions for opening
    links in a separate window or in a new tab:

    \quotefromfile webenginewidgets/simplebrowser/webview.cpp
    \skipto WebView::contextMenuEvent(
    \printuntil menu->popup
    \printline }

    \section1 Implementing WebPage Functionality

    As mentioned earlier, each \c WebView contains a \c WebPage instance that
    was created by using QWebEngineProfile::defaultProfile().

    We implement \c WebPage as a subclass of QWebEnginePage to enable HTTP,
    proxy authentication, and ignoring SSL certificate errors when accessing web
    pages:

    \quotefromfile webenginewidgets/simplebrowser/webpage.h
    \skipto WebPage :
    \printuntil }

    In all the cases above, we display the appropriate dialog to the user. In
    case of authentication, we need to set the correct credential values on the
    QAuthenticator object:

    \quotefromfile webenginewidgets/simplebrowser/webpage.cpp
    \skipto WebPage::handleAuthenticationRequired(
    \printuntil }
    \printuntil }
    \printline }

    The \c handleProxyAuthenticationRequired signal handler implements the very same
    steps for the authentication of HTTP proxies.

    In case of SSL errors, we just need to return a boolean value indicating
    whether the certificate should be ignored.

    \quotefromfile webenginewidgets/simplebrowser/webpage.cpp
    \skipto WebPage::certificateError(
    \printuntil }
    \printuntil }

    \section1 Opening a Web Page

    This section describes the workflow for opening a new page. When the user
    enters a URL in the navigation bar and presses \uicontrol Enter, the \c
    QLineEdit::returnPressed signal is emitted and the new URL is then handed
    over to \c TabWidget::setUrl:

    \quotefromfile webenginewidgets/simplebrowser/browserwindow.cpp
    \skipto connect(m_urlLineEdit
    \printline connect

    The call is forwarded to the currently selected tab:

    \quotefromfile webenginewidgets/simplebrowser/tabwidget.cpp
    \skipto TabWidget::setUrl(
    \printuntil }
    \printuntil }

    The \c setUrl() method of \c WebView just forwards the \c url to the associated \c WebPage,
    which in turn starts the downloading of the page's content in the background.

    \section1 Managing Downloads

    Downloads are associated with a \l QWebEngineProfile. Whenever a download is
    triggered on a web page the \l QWebEngineProfile::downloadRequested signal is
    emitted with a \l QWebEngineDownloadItem, which in this example is forwarded
    to \c DownloadManagerWidget::downloadRequested:

    \quotefromfile webenginewidgets/simplebrowser/browser.cpp
    \skipto Browser::Browser
    \printuntil /^\}/

    This method prompts the user for a file name (with a pre-filled suggestion)
    and starts the download (unless the user cancels the \uicontrol {Save As}
    dialog):

    \quotefromfile webenginewidgets/simplebrowser/downloadmanagerwidget.cpp
    \skipto DownloadManagerWidget::downloadRequested
    \printuntil /^\}/

    The \l QWebEngineDownloadItem object will periodically emit the \l
    {QWebEngineDownloadItem::}{downloadProgress} signal to notify potential
    observers of the download progress and the \l
    {QWebEngineDownloadItem::}{stateChanged} signal when the download is
    finished or when an error occurs. See \c downloadmanagerwidget.cpp for an
    example of how these signals can be handled.

    \section1 Licensing

    All icons used in the example, with the exception of \c{AppLogoColor.png},
    originate from the public domain
    \l{http://tango.freedesktop.org/Tango_Icon_Library}{Tango Icon Library}.
*/