Porting from Qt WebKit to Qt WebEngine▲
The following sections contain information about porting an application that uses the Qt WebKit QWebView API to use the Qt WebEngine QWebEngineView.
Architecture▲
Chromium provides its own network and painting engines, which Qt WebEngine uses. This, among other things, allows Qt WebEngine to provide better and more reliable support for the latest HTML5 specification than Qt WebKit. However, Qt WebEngine is thus also heavier than Qt WebKit and does not provide direct access to the network stack and the HTML document through C++ APIs.
Class Names▲
The Qt WebEngine equivalent of Qt WebKit C++ classes are prefixed by "QWebEngine" instead of "QWeb".
Qt WebKit
#include <QWebHistory>
#include <QWebHistoryItem>
#include <QWebPage>
#include <QWebView>
QWebHistory
QWebHistoryItem
QWebPage
QWebView
Qt WebEngine
#include <QWebEngineHistory>
#include <QWebEngineHistoryItem>
#include <QWebEnginePage>
#include <QWebEngineView>
QWebEngineHistory
QWebEngineHistoryItem
QWebEnginePage
QWebEngineView
Qt Module Name▲
In qmake Project Files▲
Qt WebKit
QT +=
webkitwidgets
Qt WebEngine
QT +=
webenginewidgets
Including the Module in Source Files▲
Qt WebKit
#include <QtWebKit/QtWebKit>
#include <QtWebKitWidgets/QtWebKitWidgets>
// With Qt >= 4.8
Qt WebEngine
#include <QtWebEngineWidgets/QtWebEngineWidgets>
QWebFrame Has Been Merged into QWebEnginePage▲
HTML frames can be used to divide web pages into several areas where the content can be represented individually.
In Qt WebKit, QWebFrame represents a frame inside a web page. Each QWebPage object contains at least one frame, the main frame, obtained using QWebPage::mainFrame(). Additional frames will be created for the HTML <frame> element, which defines the appearance and contents of a single frame, or the <iframe> element, which inserts a frame within a block of text.
In Qt WebEngine, frame handling has been merged into the QWebEnginePage class. All child frames are now considered part of the content, and only accessible through JavaScript. Methods of the QWebFrame class, such as load() are now available directly through the QWebEnginePage itself.
Qt WebKit
QWebPage page;
connect(page.mainFrame(), SIGNAL(urlChanged(const
QUrl&
amp;)), SLOT(mySlotName()));
page.mainFrame()-&
gt;load(url);
Qt WebEngine
QWebEnginePage page;
connect(&
amp;page, SIGNAL(urlChanged(const
QUrl&
amp;)), SLOT(mySlotName()));
page.load(url);
Some Methods Now Return Their Result Asynchronously▲
Because Qt WebEngine uses a multi-process architecture, calls to some methods from applications will return immediately, while the results should be received asynchronously via a callback mechanism. A function pointer, a functor, or a lambda expression must be provided to handle the results when they become available.
Qt WebKit
QWebPage *
page =
new
QWebPage;
QTextEdit *
textEdit =
new
QTextEdit;
// *textEdit is modified immediately.
textEdit-&
gt;setPlainText(page-&
gt;toHtml());
textEdit-&
gt;setPlainText(page-&
gt;toPlainText());
Qt WebEngine (with a lambda function in C++11)
QWebEnginePage *
page =
new
QWebEnginePage;
QTextEdit *
textEdit =
new
QTextEdit;
// *textEdit must remain valid until the lambda function is called.
page-&
gt;toHtml([textEdit](const
QString &
amp;result){
textEdit-&
gt;setPlainText(result); }
);
page-&
gt;toPlainText([textEdit](const
QString &
amp;result){
textEdit-&
gt;setPlainText(result); }
);
Qt WebEngine (with a functor template wrapping a member function)
template
&
lt;typename
Arg, typename
R, typename
C&
gt;
struct
InvokeWrapper {
R *
receiver;
void
(C::
*
memberFun)(Arg);
void
operator
()(Arg result) {
(receiver-&
gt;*
memberFun)(result);
}
}
;
template
&
lt;typename
Arg, typename
R, typename
C&
gt;
InvokeWrapper&
lt;Arg, R, C&
gt; invoke(R *
receiver, void
(C::
*
memberFun)(Arg))
{
InvokeWrapper&
lt;Arg, R, C&
gt; wrapper =
{
receiver, memberFun}
;
return
wrapper;
}
QWebEnginePage *
page =
new
QWebEnginePage;
QTextEdit *
textEdit =
new
QTextEdit;
// *textEdit must remain valid until the functor is called.
page-&
gt;toHtml(invoke(textEdit, &
amp;QTextEdit::
setPlainText));
page-&
gt;toPlainText(invoke(textEdit, &
amp;QTextEdit::
setPlainText));
Qt WebEngine (with a regular functor)
struct
SetPlainTextFunctor {
QTextEdit *
textEdit;
SetPlainTextFunctor(QTextEdit *
textEdit) : textEdit(textEdit) {
}
void
operator
()(const
QString &
amp;result) {
textEdit-&
gt;setPlainText(result);
}
}
;
QWebEnginePage *
page =
new
QWebEnginePage;
QTextEdit *
textEdit =
new
QTextEdit;
// *textEdit must remain valid until the functor is called.
page-&
gt;toHtml(SetPlainTextFunctor(textEdit));
page-&
gt;toPlainText(SetPlainTextFunctor(textEdit));
Qt WebEngine Does Not Interact with QNetworkAccessManager▲
Some classes of Qt Network such as QAuthenticator were reused for their interface but, unlike Qt WebKit, Qt WebEngine has its own HTTP implementation and cannot go through a QNetworkAccessManager.
The signals and methods of QNetworkAccessManager that are still supported were moved to the QWebEnginePage class.
Qt WebKit
QNetworkAccessManager qnam;
QWebPage page;
page.setNetworkAccessManager(&
amp;qnam);
connect(&
amp;qnam, SIGNAL(authenticationRequired(QNetworkReply*
,QAuthenticator*
)), this
, SLOT(authenticate(QNetworkReply*
,QAuthenticator*
)));
Qt WebEngine
QWebEnginePage page;
connect(&
amp;page, SIGNAL(authenticationRequired(QNetworkReply*
,QAuthenticator*
)), this
, SLOT(authenticate(QNetworkReply*
,QAuthenticator*
)));
In Qt WebEngine, the QAuthenticator must be explicitly set to null to cancel authentication:
*
authenticator =
QAuthenticator();
Omitting the QNetworkAccessManager also affects the way in which certificates are managed. For more information, see Managing Certificates.
Notes About Individual Methods▲
evaluateJavaScript▲
QWebFrame::evaluateJavaScript was moved and renamed as QWebEnginePage::runJavaScript. It is currently only possible to run JavaScript on the main frame of a page and the result is returned asynchronously to the provided functor.
Qt WebKit
QWebPage *
page =
new
QWebPage;
qDebug() &
lt;&
lt; page-&
gt;mainFrame()-&
gt;evaluateJavaScript("'Java' + 'Script'"
);
Qt WebEngine (with lambda expressions in C++11)
QWebEnginePage *
page =
new
QWebEnginePage;
page-&
gt;runJavaScript("'Java' + 'Script'"
, [](const
QVariant &
amp;result){
qDebug() &
lt;&
lt; result; }
);
setHtml and setContent▲
QWebEnginePage::setHtml and QWebEnginePage::setContent perform asynchronously the same way as a normal HTTP load would, unlike their QWebPage counterparts.
setContentEditable▲
QWebPage::setContentEditable has no equivalent since any document element can be made editable through the contentEditable attribute in the latest HTML standard. Therefore, QWebEnginePage::runJavaScript is all that is needed.
Qt WebKit
QWebPage page;
page.setContentEditable(true
);
Qt WebEngine
QWebEnginePage page;
page.runJavaScript("document.documentElement.contentEditable = true"
);
Unavailable Qt WebKit API▲
QGraphicsWebView |
Qt WebEngine is designed for being used with hardware acceleration. Because we could not support a web view class in a QGraphicsView unless it would be attached to a QGLWidget viewport, this feature is out of scope. |
QWebElement |
Qt WebEngine uses a multi-process architecture and this means that any access to the internal structure of the page has to be done asynchronously, any query result must be returned through callbacks. The QWebElement API was designed for synchronous access and this would require a complete redesign. |
QWebDatabase |
The Web SQL Database feature that this API was wrapping in Qt WebKit was dropped from the HTML5 standard. |
QWebPluginDatabase, QWebPluginFactory, QWebPluginInfo, QWebPage::setPalette, QWebView::setRenderHints |
Qt WebEngine renders web pages using Skia and is not using QPainter or Qt for this purpose. The HTML5 standard also now offers much better alternatives that were not available when native controls plugins were introduced in Qt WebKit. |
QWebHistoryInterface |
Visited links are persisted automatically by Qt WebEngine. |
QWebPage::setContentEditable |
In the latest HTML standard, any document element can be made editable through the contentEditable attribute. So runJavaScript is all that is needed: page->runJavaScript("document.documentElement.contentEditable = true") |
QWebPage::setLinkDelegationPolicy |
There is no way to connect a signal to run C++ code when a link is clicked. However, link clicks can be delegated to the Qt application instead of having the HTML handler engine process them by overloading the QWebEnginePage::acceptNavigationRequest() function. This is necessary when an HTML document is used as part of the user interface, and not to display external data, for example, when displaying a list of results. acceptNavigationRequest() starts the loading process and emits the loadStarted() signal before the request is accepted or rejected. Therefore, a loadFinished() signal that returns false is to be expected even after delegating the request. |