QtWebKit/QtWebEngine移植差异
原文出处:【wiki.qt.io】
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> |
Qt WebEngine |
#include <QWebEngineHistory> |
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> |
Qt WebEngine |
#include <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; |
Qt WebEngine |
QWebEnginePage page; |
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; |
Qt WebEngine (with a lambda function in C++11) |
QWebEnginePage *page = new QWebEnginePage; |
Qt WebEngine (with a functor template wrapping a member function) |
template<typename Arg, typename R, typename C> |
Qt WebEngine (with a regular functor) |
struct SetPlainTextFunctor { |
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; |
Qt WebEngine |
QWebEnginePage page; |
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; |
Qt WebEngine (with lambda expressions in C++11) |
QWebEnginePage *page = new QWebEnginePage; |
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.
QWebPage::setLinkDelegationPolicy
There is no way to connect a signal to run C++ code when a link is clicked.
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")