Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

QtWebEngine/Porting from QtWebKit: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Porting from Qt WebKit to Qt WebEngine=
[toc align_right="yes" depth="3"]


<font color="red">Important: This guide is work in progress. It will be updated together with the code in the master branch.</font>
= Porting from Qt WebKit to Qt WebEngine =


This guide gives an overview of the differences between the Qt WebKit and Qt WebEngine <span class="caps">API</span>s in applications.
{color:red}Important: This guide is work in progress. It will be updated together with the code in the master branch.


This intends to provide rough steps to follow when porting an application using Qt WebKit’s QWebView <span class="caps">API</span> to use Qt WebEngine’s QWebEngineView.
This guide gives an overview of the differences between the Qt WebKit and Qt WebEngine APIs in applications.


==Class names==
This intends to provide rough steps to follow when porting an application using Qt WebKit's QWebView API to use Qt WebEngine's QWebEngineView.
 
== Class names ==


The Qt WebEngine equivalent of Qt WebKit C++ classes are prefixed by '''QWebEngine''' instead of '''QWeb'''.
The Qt WebEngine equivalent of Qt WebKit C++ classes are prefixed by '''QWebEngine''' instead of '''QWeb'''.


==Qt module name==
|''. Qt WebKit |<br />| <code><br />#include &lt;QWebHistory&amp;gt;<br />#include &lt;QWebHistoryItem&amp;gt;<br />#include &lt;QWebPage&amp;gt;<br />#include &lt;QWebView&amp;gt;
<br />QWebHistory<br />QWebHistoryItem<br />QWebPage<br />QWebView<br /></code> |<br />|''. Qt WebEngine |<br />| <code><br />#include &lt;QWebEngineHistory&amp;gt;<br />#include &lt;QWebEngineHistoryItem&amp;gt;<br />#include &lt;QWebEnginePage&amp;gt;<br />#include &lt;QWebEngineView&amp;gt;


===In qmake project files===
QWebEngineHistory<br />QWebEngineHistoryItem<br />QWebEnginePage<br />QWebEngineView<br /></code> |


===Including the module in source files===
== Qt module name ==


==QWebFrame has been merged into QWebEnginePage==
=== In qmake project files ===


It is not possible to access sub-frames. Methods of the main QWebFrame are now available directly through the QWebEnginePage itself.
|''. Qt WebKit |<br />| <code><br />QT += webkitwidgets<br /></code> |<br />|''. Qt WebEngine |<br />| <code><br />QT ''= webenginewidgets<br /></code> |
<br />h3. Including the module in source files
<br />|''. Qt WebKit |<br />| <code><br />#include &lt;QtWebKit/QtWebKit&amp;gt;<br />#include &lt;QtWebKitWidgets/QtWebKitWidgets&amp;gt; // With Qt &gt;= 4.8<br /></code> |<br />|''. Qt WebEngine |<br />| <code><br />#include &lt;QtWebEngineWidgets/QtWebEngineWidgets&amp;gt;<br /></code> |
<br />h2. QWebFrame has been merged into QWebEnginePage
<br />It is not possible to access sub-frames. Methods of the main QWebFrame are now available directly through the QWebEnginePage itself.
<br />|''. Qt WebKit |<br />| <code><br />QWebPage page;<br />connect(page-&gt;mainFrame(), SIGNAL (urlChanged(const QUrl&amp;amp;)), SLOT (mySlotName()));<br />page.mainFrame()<s>&gt;load(url);<br /></code> |<br />|''. Qt WebEngine |<br />| <code><br />QWebEnginePage page;<br />connect(&amp;page, SIGNAL (urlChanged(const QUrl&amp;amp;)), SLOT (mySlotName()));<br />page.load(url);<br /></code> |
<br />h2. Some methods now return their result asynchronously
<br />Since Qt WebEngine uses a multi-process architecture, the application needs to return to the event loop where the result will be received asynchronously from Qt WebEngine's render process. A function pointer, a functor or a lambda expression must be provided to handle the result when it is available.
<br />|''. Qt WebKit |<br />| <code><br />QWebPage *page = new QWebPage;<br />QTextEdit *textEdit = new QTextEdit;<br />// *textEdit is modified immediately.<br />textEdit</s>&gt;setPlainText(page-&gt;toHtml());<br />textEdit-&gt;setPlainText(page-&gt;toPlainText());<br /></code> |<br />|''. Qt WebEngine (with a lambda function in C''+11) |<br />| <code><br />QWebEnginePage *page = new QWebEnginePage;<br />QTextEdit *textEdit = new QTextEdit;<br />// *textEdit must remain valid until the lambda function is called.<br />page-&gt;toHtml([textEdit](const QString &amp;result){ textEdit-&gt;setPlainText(result); });<br />page-&gt;toPlainText([textEdit](const QString &amp;result){ textEdit-&gt;setPlainText(result); });<br /></code> |<br />|''. Qt WebEngine (with a functor template wrapping a member function) |<br />| <code><br />template&amp;lt;typename Arg, typename R, typename C&amp;gt;<br />struct InvokeWrapper {<br /> R *receiver;<br /> void (C::*memberFun)(Arg);<br /> void operator()(Arg result) {<br /> (receiver-&gt;*memberFun)(result);<br /> }<br />};
<br />template&amp;lt;typename Arg, typename R, typename C&amp;gt;<br />InvokeWrapper&amp;lt;Arg, R, C&amp;gt; invoke(R *receiver, void (C::*memberFun)(Arg))<br />{<br /> InvokeWrapper&amp;lt;Arg, R, C&amp;gt; wrapper = {receiver, memberFun};<br /> return wrapper;<br />}
<br />QWebEnginePage *page = new QWebEnginePage;<br />QTextEdit *textEdit = new QTextEdit;<br />// *textEdit must remain valid until the functor is called.<br />page-&gt;toHtml(invoke(textEdit, &amp;QTextEdit::setPlainText));<br />page-&gt;toPlainText(invoke(textEdit, &amp;QTextEdit::setPlainText));<br /></code> |<br />|''. Qt WebEngine (with a regular functor) |<br />| <code><br />struct SetPlainTextFunctor {<br /> QTextEdit *textEdit;<br /> SetPlainTextFunctor(QTextEdit *textEdit) : textEdit(textEdit) { }<br /> void operator()(const QString &amp;result) {<br /> textEdit-&gt;setPlainText(result);<br /> }<br />};


==Some methods now return their result asynchronously==
QWebEnginePage *page = new QWebEnginePage;<br />QTextEdit *textEdit = new QTextEdit;<br />// *textEdit must remain valid until the functor is called.<br />page-&gt;toHtml(SetPlainTextFunctor(textEdit));<br />page-&gt;toPlainText(SetPlainTextFunctor(textEdit));<br /></code> |


Since Qt WebEngine uses a multi-process architecture, the application needs to return to the event loop where the result will be received asynchronously from Qt WebEngine’s render process. A function pointer, a functor or a lambda expression must be provided to handle the result when it is available.
== Qt WebEngine does not interact with QNetworkAccessManager ==


==Qt WebEngine does not interact with QNetworkAccessManager==
Some classes of Qt Network like QAuthenticator were reused for their interface but, unlike Qt WebKit, Qt WebEngine has its own HTTP implementation and can't go through a QNetworkAccessManager.


Some classes of Qt Network like QAuthenticator were reused for their interface but, unlike Qt WebKit, Qt WebEngine has its own <span class="caps">HTTP</span> implementation and can’t go through a QNetworkAccessManager.
Signals and methods of QNetworkAccessManager that are still supported were moved to QWebEnginePage directly.


Signals and methods of QNetworkAccessManager that are still supported were moved to QWebEnginePage directly.
|''. Qt WebKit |<br />| <code><br />QNetworkAccessManager qnam;<br />QWebPage page;<br />page.setNetworkAccessManager(&amp;qnam);<br />connect(&amp;qnam, SIGNAL (authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT (authenticate(QNetworkReply*,QAuthenticator*)));<br /></code> |<br />|''. Qt WebEngine |<br />| <code><br />QWebEnginePage page;<br />connect(&amp;page, SIGNAL (authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT (authenticate(QNetworkReply*,QAuthenticator*)));<br /></code> |


==Notes about individual methods==
== Notes about individual methods ==


===runJavaScript (QWebEnginePage)===
=== runJavaScript (QWebEnginePage) ===


QWebFrame::evaluateJavaScript was renamed and moved to the QWebEnginePage. 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.
QWebFrame::evaluateJavaScript was renamed and moved to the QWebEnginePage. 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.


===setHtml and setContent (QWebEnginePage)===
|_. Qt WebKit |<br />| <code><br />QWebPage *page = new QWebPage;<br />qDebug() &lt;&lt; page-&gt;mainFrame()<s>&gt;evaluateJavaScript(&quot;'Java' + 'Script'&quot;);<br /></code> |<br />|_. Qt WebEngine (with lambda expressions in C+''11) |<br />| <code><br />QWebEnginePage *page = new QWebEnginePage;<br />page-&gt;runJavaScript(&quot;'Java''' 'Script'&quot;, [](const QVariant &amp;result){ qDebug() &lt;&lt; result; });<br /></code> |
 
<br />h3. setHtml and setContent (QWebEnginePage)
Those methods now perform asynchronously the same way as a normal <span class="caps">HTTP</span> load would.
<br />Those methods now perform asynchronously the same way as a normal HTTP load would.
 
<br />h2. Unavailable Qt WebKit APIs
==Unavailable Qt WebKit <span class="caps">API</span>s==
<br />Qt WebKit classes or methods in this list will not be available in Qt WebEngine.
 
<br />h3. QGraphicsWebView
Qt WebKit classes or methods in this list will not be available in Qt WebEngine.
<br />Qt WebEngine requires hardware acceleration. Since we couldn't support a web view class in a QGraphicsView unless it is attached to a QGLWidget viewport, this feature is out of scope.
 
<br />h3. QWebElement
===QGraphicsWebView===
<br />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.
 
<br />h3. QWebDatabase
Qt WebEngine requires hardware acceleration. Since we couldn’t support a web view class in a QGraphicsView unless it is attached to a <span class="caps">QGLW</span>idget viewport, this feature is out of scope.
<br />The Web SQL Database feature that this API was wrapping in QtWebKit was dropped from the HTML5 standard.
 
<br />h3. QWebPluginDatabase, QWebPluginFactory, QWebPluginInfo, QWebPage::setPalette, QWebView::setRenderHints
===QWebElement===
<br />Qt WebEngine renders web pages using Skia and isn't 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 QtWebKit.
 
<br />h3. QWebHistoryInterface
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 <span class="caps">API</span> was designed for synchronous access and this would require a complete redesign.
<br />Visited links are persisted automatically by Qt WebEngine.
 
<br />h3. QWebPage::setContentEditable
===QWebDatabase===
<br />In the latest HTML standard, any document element can be made editable through the contentEditable attribute. So runJavaScript is all that is needed.<br />|<code><br />page</s>&gt;runJavascript(&quot;document.documentElement.contentEditable = true&amp;quot;)
 
The Web <span class="caps">SQL</span> Database feature that this <span class="caps">API</span> was wrapping in QtWebKit was dropped from the HTML5 standard.
 
===QWebPluginDatabase, QWebPluginFactory, QWebPluginInfo, QWebPage::setPalette, QWebView::setRenderHints===
 
Qt WebEngine renders web pages using Skia and isn’t 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 QtWebKit.
 
===QWebHistoryInterface===
 
Visited links are persisted automatically by Qt WebEngine.
 
===QWebPage::setContentEditable===
 
In the latest <span class="caps">HTML</span> standard, any document element can be made editable through the contentEditable attribute. So runJavaScript is all that is needed.<br />

Revision as of 14:26, 23 February 2015

[toc align_right="yes&quot; depth="3&quot;]

Porting from Qt WebKit to Qt WebEngine

{color:red}Important: This guide is work in progress. It will be updated together with the code in the master branch.

This guide gives an overview of the differences between the Qt WebKit and Qt WebEngine APIs in applications.

This intends to provide rough steps to follow when porting an application using Qt WebKit's QWebView API to use Qt WebEngine's QWebEngineView.

Class names

The Qt WebEngine equivalent of Qt WebKit C++ classes are prefixed by QWebEngine instead of QWeb.

|. Qt WebKit |
|

<br />#include &lt;QWebHistory&amp;gt;<br />#include &lt;QWebHistoryItem&amp;gt;<br />#include &lt;QWebPage&amp;gt;<br />#include &lt;QWebView&amp;gt;
<br />QWebHistory<br />QWebHistoryItem<br />QWebPage<br />QWebView<br />

|
|
. Qt WebEngine |
|

<br />#include &lt;QWebEngineHistory&amp;gt;<br />#include &lt;QWebEngineHistoryItem&amp;gt;<br />#include &lt;QWebEnginePage&amp;gt;<br />#include &lt;QWebEngineView&amp;gt;

QWebEngineHistory<br />QWebEngineHistoryItem<br />QWebEnginePage<br />QWebEngineView<br />

|

Qt module name

In qmake project files

|. Qt WebKit |
|

<br />QT += webkitwidgets<br />

|
|
. Qt WebEngine |
|

<br />QT ''= webenginewidgets<br />

|


h3. Including the module in source files


|. Qt WebKit |
|

<br />#include &lt;QtWebKit/QtWebKit&amp;gt;<br />#include &lt;QtWebKitWidgets/QtWebKitWidgets&amp;gt; // With Qt &gt;= 4.8<br />

|
|
. Qt WebEngine |
|

<br />#include &lt;QtWebEngineWidgets/QtWebEngineWidgets&amp;gt;<br />

|


h2. QWebFrame has been merged into QWebEnginePage
It is not possible to access sub-frames. Methods of the main QWebFrame are now available directly through the QWebEnginePage itself.


|. Qt WebKit |
|

<br />QWebPage page;<br />connect(page-&gt;mainFrame(), SIGNAL (urlChanged(const QUrl&amp;amp;)), SLOT (mySlotName()));<br />page.mainFrame()<s>&gt;load(url);<br />

|
|
. Qt WebEngine |
|

<br />QWebEnginePage page;<br />connect(&amp;page, SIGNAL (urlChanged(const QUrl&amp;amp;)), SLOT (mySlotName()));<br />page.load(url);<br />

|


h2. Some methods now return their result asynchronously
Since Qt WebEngine uses a multi-process architecture, the application needs to return to the event loop where the result will be received asynchronously from Qt WebEngine's render process. A function pointer, a functor or a lambda expression must be provided to handle the result when it is available.


|. Qt WebKit |
|

<br />QWebPage *page = new QWebPage;<br />QTextEdit *textEdit = new QTextEdit;<br />// *textEdit is modified immediately.<br />textEdit</s>&gt;setPlainText(page-&gt;toHtml());<br />textEdit-&gt;setPlainText(page-&gt;toPlainText());<br />

|
|
. Qt WebEngine (with a lambda function in C+11) |
|

<br />QWebEnginePage *page = new QWebEnginePage;<br />QTextEdit *textEdit = new QTextEdit;<br />// *textEdit must remain valid until the lambda function is called.<br />page-&gt;toHtml([textEdit](const QString &amp;result){ textEdit-&gt;setPlainText(result); });<br />page-&gt;toPlainText([textEdit](const QString &amp;result){ textEdit-&gt;setPlainText(result); });<br />

|
|
. Qt WebEngine (with a functor template wrapping a member function) |
|

<br />template&amp;lt;typename Arg, typename R, typename C&amp;gt;<br />struct InvokeWrapper {<br /> R *receiver;<br /> void (C::*memberFun)(Arg);<br /> void operator()(Arg result) {<br /> (receiver-&gt;*memberFun)(result);<br /> }<br />};
<br />template&amp;lt;typename Arg, typename R, typename C&amp;gt;<br />InvokeWrapper&amp;lt;Arg, R, C&amp;gt; invoke(R *receiver, void (C::*memberFun)(Arg))<br />{<br /> InvokeWrapper&amp;lt;Arg, R, C&amp;gt; wrapper = {receiver, memberFun};<br /> return wrapper;<br />}
<br />QWebEnginePage *page = new QWebEnginePage;<br />QTextEdit *textEdit = new QTextEdit;<br />// *textEdit must remain valid until the functor is called.<br />page-&gt;toHtml(invoke(textEdit, &amp;QTextEdit::setPlainText));<br />page-&gt;toPlainText(invoke(textEdit, &amp;QTextEdit::setPlainText));<br />

|
|. Qt WebEngine (with a regular functor) |
|

<br />struct SetPlainTextFunctor {<br /> QTextEdit *textEdit;<br /> SetPlainTextFunctor(QTextEdit *textEdit) : textEdit(textEdit) { }<br /> void operator()(const QString &amp;result) {<br /> textEdit-&gt;setPlainText(result);<br /> }<br />};

QWebEnginePage *page = new QWebEnginePage;<br />QTextEdit *textEdit = new QTextEdit;<br />// *textEdit must remain valid until the functor is called.<br />page-&gt;toHtml(SetPlainTextFunctor(textEdit));<br />page-&gt;toPlainText(SetPlainTextFunctor(textEdit));<br />

|

Qt WebEngine does not interact with QNetworkAccessManager

Some classes of Qt Network like QAuthenticator were reused for their interface but, unlike Qt WebKit, Qt WebEngine has its own HTTP implementation and can't go through a QNetworkAccessManager.

Signals and methods of QNetworkAccessManager that are still supported were moved to QWebEnginePage directly.

|. Qt WebKit |
|

<br />QNetworkAccessManager qnam;<br />QWebPage page;<br />page.setNetworkAccessManager(&amp;qnam);<br />connect(&amp;qnam, SIGNAL (authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT (authenticate(QNetworkReply*,QAuthenticator*)));<br />

|
|
. Qt WebEngine |
|

<br />QWebEnginePage page;<br />connect(&amp;page, SIGNAL (authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT (authenticate(QNetworkReply*,QAuthenticator*)));<br />

|

Notes about individual methods

runJavaScript (QWebEnginePage)

QWebFrame::evaluateJavaScript was renamed and moved to the QWebEnginePage. 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 |
|

<br />QWebPage *page = new QWebPage;<br />qDebug() &lt;&lt; page-&gt;mainFrame()<s>&gt;evaluateJavaScript(&quot;'Java' + 'Script'&quot;);<br />

|
|_. Qt WebEngine (with lambda expressions in C+11) |
|

<br />QWebEnginePage *page = new QWebEnginePage;<br />page-&gt;runJavaScript(&quot;'Java''' 'Script'&quot;, [](const QVariant &amp;result){ qDebug() &lt;&lt; result; });<br />

|


h3. setHtml and setContent (QWebEnginePage)
Those methods now perform asynchronously the same way as a normal HTTP load would.
h2. Unavailable Qt WebKit APIs
Qt WebKit classes or methods in this list will not be available in Qt WebEngine.
h3. QGraphicsWebView
Qt WebEngine requires hardware acceleration. Since we couldn't support a web view class in a QGraphicsView unless it is attached to a QGLWidget viewport, this feature is out of scope.
h3. 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.
h3. QWebDatabase
The Web SQL Database feature that this API was wrapping in QtWebKit was dropped from the HTML5 standard.
h3. QWebPluginDatabase, QWebPluginFactory, QWebPluginInfo, QWebPage::setPalette, QWebView::setRenderHints
Qt WebEngine renders web pages using Skia and isn't 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 QtWebKit.
h3. QWebHistoryInterface
Visited links are persisted automatically by Qt WebEngine.
h3. 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&quot;)