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
(Convert ExpressionEngine section headers)
No edit summary
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}


[toc align_right="yes" depth="3"]


= Porting from Qt WebKit to Qt WebEngine =
= Porting from Qt WebKit to Qt WebEngine =
 
<span style="color: red"> Important: This guide is work in progress. It will be updated together with the code in the master branch. </span>
{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 guide gives an overview of the differences between the Qt WebKit and Qt WebEngine APIs in applications.
Line 15: Line 13:
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 WebKit |
{|
! style="text-align:left;"| Qt WebKit
|-
| <code>
| <code>
#include <QWebHistory>
#include <QWebHistory>
Line 26: Line 26:
QWebPage
QWebPage
QWebView
QWebView
</code> |
</code>
|''. Qt WebEngine |
|-
! style="text-align:left;"| Qt WebEngine
|-
| <code>
| <code>
#include <QWebEngineHistory>
#include <QWebEngineHistory>
Line 38: Line 40:
QWebEnginePage
QWebEnginePage
QWebEngineView
QWebEngineView
</code> |
</code>
|}


== Qt module name ==
== Qt module name ==
Line 44: Line 47:
=== In qmake project files ===
=== In qmake project files ===


|''. Qt WebKit |
{|
! style="text-align:left;"| Qt WebKit
|-
| <code>
| <code>
QT += webkitwidgets
QT += webkitwidgets
</code> |
</code>
|''. Qt WebEngine |
|-
! style="text-align:left;"| Qt WebEngine
|-
| <code>
| <code>
QT ''= webenginewidgets
QT += webenginewidgets
</code> |
</code>
|}


=== Including the module in source files ===
=== Including the module in source files ===
|''. Qt WebKit |
 
{|
! style="text-align:left;"| Qt WebKit
|-
| <code>
| <code>
#include <QtWebKit/QtWebKit>
#include <QtWebKit/QtWebKit>
#include <QtWebKitWidgets/QtWebKitWidgets> // With Qt >= 4.8
#include <QtWebKitWidgets/QtWebKitWidgets> // With Qt >= 4.8
</code> |
</code>
|''. Qt WebEngine |
|-
! style="text-align:left;"| Qt WebEngine
|-
| <code>
| <code>
#include <QtWebEngineWidgets/QtWebEngineWidgets>
#include <QtWebEngineWidgets/QtWebEngineWidgets>
</code> |
</code>
|}


== QWebFrame has been merged into QWebEnginePage ==
== 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.
It is not possible to access sub-frames. Methods of the main QWebFrame are now available directly through the QWebEnginePage itself.


|''. Qt WebKit |
{|
! style="text-align:left;"|Qt WebKit
|-
| <code>
| <code>
QWebPage page;
QWebPage page;
connect(page->mainFrame(), SIGNAL (urlChanged(const QUrl&amp;amp;)), SLOT (mySlotName()));
connect(page->mainFrame(), SIGNAL (urlChanged(const QUrl&)), SLOT (mySlotName()));
page.mainFrame()->load(url);
page.mainFrame()->load(url);
</code> |
</code>
|''. Qt WebEngine |
|-
! style="text-align:left;"| Qt WebEngine
|-
| <code>
| <code>
QWebEnginePage page;
QWebEnginePage page;
connect(&amp;page, SIGNAL (urlChanged(const QUrl&amp;amp;)), SLOT (mySlotName()));
connect(%page, SIGNAL (urlChanged(const QUrl&)), SLOT (mySlotName()));
page.load(url);
page.load(url);
</code> |
</code>
|}


== Some methods now return their result asynchronously ==
== 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.
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 |
{|
! style="text-align:left;"| Qt WebKit
|-
| <code>
| <code>
QWebPage *page = new QWebPage;
QWebPage *page = new QWebPage;
Line 90: Line 112:
textEdit->setPlainText(page->toHtml());
textEdit->setPlainText(page->toHtml());
textEdit->setPlainText(page->toPlainText());
textEdit->setPlainText(page->toPlainText());
</code> |
</code>
|''. Qt WebEngine (with a lambda function in C''+11) |
|-
! style="text-align:left;"| Qt WebEngine (with a lambda function in C''+11)
|-
| <code>
| <code>
QWebEnginePage *page = new QWebEnginePage;
QWebEnginePage *page = new QWebEnginePage;
QTextEdit *textEdit = new QTextEdit;
QTextEdit *textEdit = new QTextEdit;
// *textEdit must remain valid until the lambda function is called.
// *textEdit must remain valid until the lambda function is called.
page->toHtml([textEdit](const QString &amp;result){ textEdit->setPlainText(result); });
page->toHtml([textEdit](const QString &result){ textEdit->setPlainText(result); });
page->toPlainText([textEdit](const QString &amp;result){ textEdit->setPlainText(result); });
page->toPlainText([textEdit](const QString &result){ textEdit->setPlainText(result); });
</code> |
</code>  
|''. Qt WebEngine (with a functor template wrapping a member function) |
|-
! style="text-align:left;"|Qt WebEngine (with a functor template wrapping a member function)
|-
| <code>
| <code>
template<typename Arg, typename R, typename C>
template<typename Arg, typename R, typename C>
Line 120: Line 146:
QTextEdit *textEdit = new QTextEdit;
QTextEdit *textEdit = new QTextEdit;
// *textEdit must remain valid until the functor is called.
// *textEdit must remain valid until the functor is called.
page->toHtml(invoke(textEdit, &amp;QTextEdit::setPlainText));
page->toHtml(invoke(textEdit, &QTextEdit::setPlainText));
page->toPlainText(invoke(textEdit, &amp;QTextEdit::setPlainText));
page->toPlainText(invoke(textEdit, &QTextEdit::setPlainText));
</code> |
</code>
|''. Qt WebEngine (with a regular functor) |
|-
! style="text-align:left;"| Qt WebEngine (with a regular functor)
|-
| <code>
| <code>
struct SetPlainTextFunctor {
struct SetPlainTextFunctor {
  QTextEdit *textEdit;
  QTextEdit *textEdit;
  SetPlainTextFunctor(QTextEdit *textEdit) : textEdit(textEdit) { }
  SetPlainTextFunctor(QTextEdit *textEdit) : textEdit(textEdit) { }
  void operator()(const QString &amp;result) {
  void operator()(const QString &result) {
  textEdit->setPlainText(result);
  textEdit->setPlainText(result);
  }
  }
Line 138: Line 166:
page->toHtml(SetPlainTextFunctor(textEdit));
page->toHtml(SetPlainTextFunctor(textEdit));
page->toPlainText(SetPlainTextFunctor(textEdit));
page->toPlainText(SetPlainTextFunctor(textEdit));
</code> |
</code>
|}


== Qt WebEngine does not interact with QNetworkAccessManager ==
== Qt WebEngine does not interact with QNetworkAccessManager ==

Revision as of 08:42, 6 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.


Porting from Qt WebKit to Qt WebEngine

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
#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

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

Qt WebKit
QWebPage page;
connect(page->mainFrame(), SIGNAL (urlChanged(const QUrl&)), SLOT (mySlotName()));
page.mainFrame()->load(url);
Qt WebEngine
QWebEnginePage page;
connect(%page, SIGNAL (urlChanged(const QUrl&)), SLOT (mySlotName()));
page.load(url);

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
QWebPage *page = new QWebPage;
QTextEdit *textEdit = new QTextEdit;
// *textEdit is modified immediately.
textEdit->setPlainText(page->toHtml());
textEdit->setPlainText(page->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->toHtml([textEdit](const QString &result){ textEdit->setPlainText(result); });
page->toPlainText([textEdit](const QString &result){ textEdit->setPlainText(result); });
Qt WebEngine (with a functor template wrapping a member function)
template<typename Arg, typename R, typename C>
struct InvokeWrapper {
 R *receiver;
 void (C::*memberFun)(Arg);
 void operator()(Arg result) {
 (receiver->*memberFun)(result);
 }
};

template<typename Arg, typename R, typename C>
InvokeWrapper<Arg, R, C> invoke(R *receiver, void (C::*memberFun)(Arg))
{
 InvokeWrapper<Arg, R, C> wrapper = {receiver, memberFun};
 return wrapper;
}

QWebEnginePage *page = new QWebEnginePage;
QTextEdit *textEdit = new QTextEdit;
// *textEdit must remain valid until the functor is called.
page->toHtml(invoke(textEdit, &QTextEdit::setPlainText));
page->toPlainText(invoke(textEdit, &QTextEdit::setPlainText));
Qt WebEngine (with a regular functor)
struct SetPlainTextFunctor {
 QTextEdit *textEdit;
 SetPlainTextFunctor(QTextEdit *textEdit) : textEdit(textEdit) { }
 void operator()(const QString &result) {
 textEdit->setPlainText(result);
 }
};

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

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 |

|

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*)));

|

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 |

|

QWebPage *page = new QWebPage;
qDebug() << page->mainFrame()->evaluateJavaScript("'Java' + 'Script'");

|

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

|

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

|

setHtml and setContent (QWebEnginePage)

Those methods now perform asynchronously the same way as a normal HTTP load would.

Unavailable Qt WebKit APIs

Qt WebKit classes or methods in this list will not be available in Qt WebEngine.

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.

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 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 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")