<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>webpage.cpp Example File | Qt WebEngine 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtwebengine-index.html">Qt WebEngine</a></td><td ><a href="qtwebengine-webenginewidgets-simplebrowser-example.html">WebEngine Widgets Simple Browser Example</a></td><td >webpage.cpp Example File</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtwebengine-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">webpage.cpp Example File</h1> <span class="subtitle">webenginewidgets/simplebrowser/webpage.cpp</span> <!-- $$$webenginewidgets/simplebrowser/webpage.cpp-description --> <div class="descr"> <a name="details"></a> <pre class="cpp"> <span class="comment">/**************************************************************************** ** ** Copyright (C) 2016 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$ ** ****************************************************************************/</span> <span class="preprocessor">#include "browserwindow.h"</span> <span class="preprocessor">#include "tabwidget.h"</span> <span class="preprocessor">#include "ui_certificateerrordialog.h"</span> <span class="preprocessor">#include "ui_passworddialog.h"</span> <span class="preprocessor">#include "webpage.h"</span> <span class="preprocessor">#include "webview.h"</span> <span class="preprocessor">#include <QAuthenticator></span> <span class="preprocessor">#include <QMessageBox></span> <span class="preprocessor">#include <QStyle></span> <span class="preprocessor">#include <QWebEngineCertificateError></span> WebPage<span class="operator">::</span>WebPage(<span class="type"><a href="qwebengineprofile.html">QWebEngineProfile</a></span> <span class="operator">*</span>profile<span class="operator">,</span> <span class="type">QObject</span> <span class="operator">*</span>parent) : <span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span>(profile<span class="operator">,</span> parent) { connect(<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>authenticationRequired<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>WebPage<span class="operator">::</span>handleAuthenticationRequired); connect(<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>featurePermissionRequested<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>WebPage<span class="operator">::</span>handleFeaturePermissionRequested); connect(<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>proxyAuthenticationRequired<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>WebPage<span class="operator">::</span>handleProxyAuthenticationRequired); connect(<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>registerProtocolHandlerRequested<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>WebPage<span class="operator">::</span>handleRegisterProtocolHandlerRequested); <span class="preprocessor">#if !defined(QT_NO_SSL) || QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)</span> connect(<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>selectClientCertificate<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>WebPage<span class="operator">::</span>handleSelectClientCertificate); <span class="preprocessor">#endif</span> } bool WebPage<span class="operator">::</span>certificateError(<span class="keyword">const</span> <span class="type"><a href="qwebenginecertificateerror.html">QWebEngineCertificateError</a></span> <span class="operator">&</span>error) { <span class="type">QWidget</span> <span class="operator">*</span>mainWindow <span class="operator">=</span> view()<span class="operator">-</span><span class="operator">></span>window(); <span class="keyword">if</span> (error<span class="operator">.</span>isOverridable()) { <span class="type">QDialog</span> dialog(mainWindow); dialog<span class="operator">.</span>setModal(<span class="keyword">true</span>); dialog<span class="operator">.</span>setWindowFlags(dialog<span class="operator">.</span>windowFlags() <span class="operator">&</span> <span class="operator">~</span><span class="type">Qt</span><span class="operator">::</span>WindowContextHelpButtonHint); Ui<span class="operator">::</span>CertificateErrorDialog certificateDialog; certificateDialog<span class="operator">.</span>setupUi(<span class="operator">&</span>dialog); certificateDialog<span class="operator">.</span>m_iconLabel<span class="operator">-</span><span class="operator">></span>setText(<span class="type">QString</span>()); <span class="type">QIcon</span> icon(mainWindow<span class="operator">-</span><span class="operator">></span>style()<span class="operator">-</span><span class="operator">></span>standardIcon(<span class="type">QStyle</span><span class="operator">::</span>SP_MessageBoxWarning<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> mainWindow)); certificateDialog<span class="operator">.</span>m_iconLabel<span class="operator">-</span><span class="operator">></span>setPixmap(icon<span class="operator">.</span>pixmap(<span class="number">32</span><span class="operator">,</span> <span class="number">32</span>)); certificateDialog<span class="operator">.</span>m_errorLabel<span class="operator">-</span><span class="operator">></span>setText(error<span class="operator">.</span>errorDescription()); dialog<span class="operator">.</span>setWindowTitle(tr(<span class="string">"Certificate Error"</span>)); <span class="keyword">return</span> dialog<span class="operator">.</span>exec() <span class="operator">=</span><span class="operator">=</span> <span class="type">QDialog</span><span class="operator">::</span>Accepted; } <span class="type">QMessageBox</span><span class="operator">::</span>critical(mainWindow<span class="operator">,</span> tr(<span class="string">"Certificate Error"</span>)<span class="operator">,</span> error<span class="operator">.</span>errorDescription()); <span class="keyword">return</span> <span class="keyword">false</span>; } <span class="type">void</span> WebPage<span class="operator">::</span>handleAuthenticationRequired(<span class="keyword">const</span> <span class="type">QUrl</span> <span class="operator">&</span>requestUrl<span class="operator">,</span> <span class="type">QAuthenticator</span> <span class="operator">*</span>auth) { <span class="type">QWidget</span> <span class="operator">*</span>mainWindow <span class="operator">=</span> view()<span class="operator">-</span><span class="operator">></span>window(); <span class="type">QDialog</span> dialog(mainWindow); dialog<span class="operator">.</span>setModal(<span class="keyword">true</span>); dialog<span class="operator">.</span>setWindowFlags(dialog<span class="operator">.</span>windowFlags() <span class="operator">&</span> <span class="operator">~</span><span class="type">Qt</span><span class="operator">::</span>WindowContextHelpButtonHint); Ui<span class="operator">::</span>PasswordDialog passwordDialog; passwordDialog<span class="operator">.</span>setupUi(<span class="operator">&</span>dialog); passwordDialog<span class="operator">.</span>m_iconLabel<span class="operator">-</span><span class="operator">></span>setText(<span class="type">QString</span>()); <span class="type">QIcon</span> icon(mainWindow<span class="operator">-</span><span class="operator">></span>style()<span class="operator">-</span><span class="operator">></span>standardIcon(<span class="type">QStyle</span><span class="operator">::</span>SP_MessageBoxQuestion<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> mainWindow)); passwordDialog<span class="operator">.</span>m_iconLabel<span class="operator">-</span><span class="operator">></span>setPixmap(icon<span class="operator">.</span>pixmap(<span class="number">32</span><span class="operator">,</span> <span class="number">32</span>)); <span class="type">QString</span> introMessage(tr(<span class="string">"Enter username and password for \"%1\" at %2"</span>) <span class="operator">.</span>arg(auth<span class="operator">-</span><span class="operator">></span>realm())<span class="operator">.</span>arg(requestUrl<span class="operator">.</span>toString()<span class="operator">.</span>toHtmlEscaped())); passwordDialog<span class="operator">.</span>m_infoLabel<span class="operator">-</span><span class="operator">></span>setText(introMessage); passwordDialog<span class="operator">.</span>m_infoLabel<span class="operator">-</span><span class="operator">></span>setWordWrap(<span class="keyword">true</span>); <span class="keyword">if</span> (dialog<span class="operator">.</span>exec() <span class="operator">=</span><span class="operator">=</span> <span class="type">QDialog</span><span class="operator">::</span>Accepted) { auth<span class="operator">-</span><span class="operator">></span>setUser(passwordDialog<span class="operator">.</span>m_userNameLineEdit<span class="operator">-</span><span class="operator">></span>text()); auth<span class="operator">-</span><span class="operator">></span>setPassword(passwordDialog<span class="operator">.</span>m_passwordLineEdit<span class="operator">-</span><span class="operator">></span>text()); } <span class="keyword">else</span> { <span class="comment">// Set authenticator null if dialog is cancelled</span> <span class="operator">*</span>auth <span class="operator">=</span> <span class="type">QAuthenticator</span>(); } } <span class="keyword">inline</span> <span class="type">QString</span> questionForFeature(<span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>Feature feature) { <span class="keyword">switch</span> (feature) { <span class="keyword">case</span> <span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>Geolocation: <span class="keyword">return</span> WebPage<span class="operator">::</span>tr(<span class="string">"Allow %1 to access your location information?"</span>); <span class="keyword">case</span> <span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>MediaAudioCapture: <span class="keyword">return</span> WebPage<span class="operator">::</span>tr(<span class="string">"Allow %1 to access your microphone?"</span>); <span class="keyword">case</span> <span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>MediaVideoCapture: <span class="keyword">return</span> WebPage<span class="operator">::</span>tr(<span class="string">"Allow %1 to access your webcam?"</span>); <span class="keyword">case</span> <span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>MediaAudioVideoCapture: <span class="keyword">return</span> WebPage<span class="operator">::</span>tr(<span class="string">"Allow %1 to access your microphone and webcam?"</span>); <span class="keyword">case</span> <span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>MouseLock: <span class="keyword">return</span> WebPage<span class="operator">::</span>tr(<span class="string">"Allow %1 to lock your mouse cursor?"</span>); <span class="keyword">case</span> <span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>DesktopVideoCapture: <span class="keyword">return</span> WebPage<span class="operator">::</span>tr(<span class="string">"Allow %1 to capture video of your desktop?"</span>); <span class="keyword">case</span> <span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>DesktopAudioVideoCapture: <span class="keyword">return</span> WebPage<span class="operator">::</span>tr(<span class="string">"Allow %1 to capture audio and video of your desktop?"</span>); <span class="keyword">case</span> <span class="type"><a href="qwebenginepage.html">QWebEnginePage</a></span><span class="operator">::</span>Notifications: <span class="keyword">return</span> <span class="type">QString</span>(); } <span class="keyword">return</span> <span class="type">QString</span>(); } <span class="type">void</span> WebPage<span class="operator">::</span>handleFeaturePermissionRequested(<span class="keyword">const</span> <span class="type">QUrl</span> <span class="operator">&</span>securityOrigin<span class="operator">,</span> Feature feature) { <span class="type">QString</span> title <span class="operator">=</span> tr(<span class="string">"Permission Request"</span>); <span class="type">QString</span> question <span class="operator">=</span> questionForFeature(feature)<span class="operator">.</span>arg(securityOrigin<span class="operator">.</span>host()); <span class="keyword">if</span> (<span class="operator">!</span>question<span class="operator">.</span>isEmpty() <span class="operator">&</span><span class="operator">&</span> <span class="type">QMessageBox</span><span class="operator">::</span>question(view()<span class="operator">-</span><span class="operator">></span>window()<span class="operator">,</span> title<span class="operator">,</span> question) <span class="operator">=</span><span class="operator">=</span> <span class="type">QMessageBox</span><span class="operator">::</span>Yes) setFeaturePermission(securityOrigin<span class="operator">,</span> feature<span class="operator">,</span> PermissionGrantedByUser); <span class="keyword">else</span> setFeaturePermission(securityOrigin<span class="operator">,</span> feature<span class="operator">,</span> PermissionDeniedByUser); } <span class="type">void</span> WebPage<span class="operator">::</span>handleProxyAuthenticationRequired(<span class="keyword">const</span> <span class="type">QUrl</span> <span class="operator">&</span><span class="operator">,</span> <span class="type">QAuthenticator</span> <span class="operator">*</span>auth<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>proxyHost) { <span class="type">QWidget</span> <span class="operator">*</span>mainWindow <span class="operator">=</span> view()<span class="operator">-</span><span class="operator">></span>window(); <span class="type">QDialog</span> dialog(mainWindow); dialog<span class="operator">.</span>setModal(<span class="keyword">true</span>); dialog<span class="operator">.</span>setWindowFlags(dialog<span class="operator">.</span>windowFlags() <span class="operator">&</span> <span class="operator">~</span><span class="type">Qt</span><span class="operator">::</span>WindowContextHelpButtonHint); Ui<span class="operator">::</span>PasswordDialog passwordDialog; passwordDialog<span class="operator">.</span>setupUi(<span class="operator">&</span>dialog); passwordDialog<span class="operator">.</span>m_iconLabel<span class="operator">-</span><span class="operator">></span>setText(<span class="type">QString</span>()); <span class="type">QIcon</span> icon(mainWindow<span class="operator">-</span><span class="operator">></span>style()<span class="operator">-</span><span class="operator">></span>standardIcon(<span class="type">QStyle</span><span class="operator">::</span>SP_MessageBoxQuestion<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> mainWindow)); passwordDialog<span class="operator">.</span>m_iconLabel<span class="operator">-</span><span class="operator">></span>setPixmap(icon<span class="operator">.</span>pixmap(<span class="number">32</span><span class="operator">,</span> <span class="number">32</span>)); <span class="type">QString</span> introMessage <span class="operator">=</span> tr(<span class="string">"Connect to proxy \"%1\" using:"</span>); introMessage <span class="operator">=</span> introMessage<span class="operator">.</span>arg(proxyHost<span class="operator">.</span>toHtmlEscaped()); passwordDialog<span class="operator">.</span>m_infoLabel<span class="operator">-</span><span class="operator">></span>setText(introMessage); passwordDialog<span class="operator">.</span>m_infoLabel<span class="operator">-</span><span class="operator">></span>setWordWrap(<span class="keyword">true</span>); <span class="keyword">if</span> (dialog<span class="operator">.</span>exec() <span class="operator">=</span><span class="operator">=</span> <span class="type">QDialog</span><span class="operator">::</span>Accepted) { auth<span class="operator">-</span><span class="operator">></span>setUser(passwordDialog<span class="operator">.</span>m_userNameLineEdit<span class="operator">-</span><span class="operator">></span>text()); auth<span class="operator">-</span><span class="operator">></span>setPassword(passwordDialog<span class="operator">.</span>m_passwordLineEdit<span class="operator">-</span><span class="operator">></span>text()); } <span class="keyword">else</span> { <span class="comment">// Set authenticator null if dialog is cancelled</span> <span class="operator">*</span>auth <span class="operator">=</span> <span class="type">QAuthenticator</span>(); } } <span class="type">void</span> WebPage<span class="operator">::</span>handleRegisterProtocolHandlerRequested(<span class="type"><a href="qwebengineregisterprotocolhandlerrequest.html">QWebEngineRegisterProtocolHandlerRequest</a></span> request) { <span class="keyword">auto</span> answer <span class="operator">=</span> <span class="type">QMessageBox</span><span class="operator">::</span>question( view()<span class="operator">-</span><span class="operator">></span>window()<span class="operator">,</span> tr(<span class="string">"Permission Request"</span>)<span class="operator">,</span> tr(<span class="string">"Allow %1 to open all %2 links?"</span>) <span class="operator">.</span>arg(request<span class="operator">.</span>origin()<span class="operator">.</span>host()) <span class="operator">.</span>arg(request<span class="operator">.</span>scheme())); <span class="keyword">if</span> (answer <span class="operator">=</span><span class="operator">=</span> <span class="type">QMessageBox</span><span class="operator">::</span>Yes) request<span class="operator">.</span>accept(); <span class="keyword">else</span> request<span class="operator">.</span>reject(); } <span class="preprocessor">#if !defined(QT_NO_SSL) || QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)</span> <span class="type">void</span> WebPage<span class="operator">::</span>handleSelectClientCertificate(<span class="type"><a href="qwebengineclientcertificateselection.html">QWebEngineClientCertificateSelection</a></span> selection) { <span class="comment">// Just select one.</span> selection<span class="operator">.</span>select(selection<span class="operator">.</span>certificates()<span class="operator">.</span>at(<span class="number">0</span>)); } <span class="preprocessor">#endif</span> </pre> </div> <!-- @@@webenginewidgets/simplebrowser/webpage.cpp --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>