Qt WebEngine 特徵

Qt WebEngine 支持下列特徵:

音頻和視頻編解碼器

Qt WebEngine 纔支持 MPEG-4 Part 14 (MP4) 文件格式若所需專有音頻 視頻編解碼器 (如 H.264 和 MPEG layer-3 (MP3)) 被啓用。專有編解碼器可以被啓用通過將以下選項傳遞給 configure 工具當配置 Qt 時:

-webengine-proprietary-codecs
					

例如,傳遞以下選項當在頂層配置 Qt 以構建它時:

configure -webengine-proprietary-codecs
					

更多信息,見 Qt 配置選項 .

當使用 qmake 僅僅構建 Qt WebEngine 模塊時,可以使用以下命令 (在此範例中,Qt WebEngine 源代碼位於 C:\qt\qtwebengine ):

qmake C:\qt\qtwebengine -- -webengine-proprietary-codecs
					

警告: 當分發專有編解碼器庫時,它們必須獲得許可。

FFmpeg 是錄製、轉換及流化音頻 視頻的跨平颱解決方案。可以將其配置為與多個編解碼器一起使用,但帶編解碼器庫分發時會引發許可問題。對於某些編解碼器,開源實現,譬如 OpenH264 ,是可用的。

Chromium DevTools

Chromium DevTools 提供審查 調試任何 Web 內容的布局和性能問題的能力。

可以測試此特徵通過啓動 Qt WebEngine 應用程序采用命令行選項 --remote-debugging-port=[your-port] 或通過設置環境變量 QTWEBENGINE_REMOTE_DEBUGGING ,然後使用基於 Chromium 的瀏覽器 (譬如 簡單瀏覽器 or 納米瀏覽器 ) 連接到 http://localhost:[your-port] .

The Chromium DevTools page can also be shown within the application. To set this up, you can call either QWebEnginePage::setInspectedPage () to the page to be inspected, which implicitly loads the DevTools into the this page, or QWebEnginePage::setDevToolsPage () to let the this page be inspected.

The respective QML properties are WebEngineView.devToolsView and WebEngineView.inspectedView .

更多信息,見 Qt WebEngine 調試和剖分析 .

客戶端證書

Some web servers, in particular many intranet sites, require the client to authenticate itself with a certificate, called a client certificate . Qt WebEngine will read the client certificates installed in the system settings in macOS and Windows, and on Linux those installed into the NSS database. Certificates can be installed into the NSS database using the pk12util 工具。

By default, Qt WebEngine will not offer any client certificates to servers, as doing so uniquely identifies the user and might violate privacy expectations.

To activate support for client certificates, an application needs to listen to the QWebEnginePage::selectClientCertificate or WebEngineView.selectClientCertificate signals and select one of the offered certificates. For applications that can navigate to untrusted web sites, it is recommended to always give the user a choice before uniquely identifying them to a remote server.

自定義方案

Qt WebEngine makes it possible for the application to define its own custom URL schemes with specialized security policies and transport mechanisms.

Custom schemes can be used to implement alternative network protocols with all the usual web security policies, privileged internal schemes for displaying user interface compoments or debugging information, sandboxed schemes with extra restrictions, and so on.

更多信息,見 QWebEngineUrlScheme and QWebEngineUrlSchemeHandler .

拖放

Qt WebEngine 支持 HTML5 拖放。

可以通過打開 HTML5 拖放演示來測試此特徵,譬如 HTML5 演示 - 拖放 , HTML5 演示 - 簡單拖放 ,或 HTML5 演示 - 拖放,自動上傳 , in 簡單瀏覽器 or 納米瀏覽器 .

把文件拖拽到瀏覽器中實際上不屬於 HTML5,但它是受支持的。可以測試它通過打開 HTML5 演示 - 文件 API .

該特徵支持在 Qt 5.7.0 添加。

Fullscreen

Qt WebEngine supports viewing web content in fullscreen mode. For more information, see WebEngineSettings.fullscreenSupportEnabled , WebEngineView.fullScreenRequested , QWebEngineSettings::FullScreenSupportEnabled ,和 QWebEnginePage::fullScreenRequested .

This feature can be tested by playing a video from YouTube in Video Player or 納米瀏覽器 , and clicking the full screen icon to go into fullscreen mode.

該特徵支持在 Qt 5.6.0 添加。

HTML5 DRM

Qt WebEngine 支持查看 DRM (數字版權管理) 保護視頻若 Widevine CDM plugin has been installed. CDM plugin is a replacement of Flash based plugins for displaying DRM-protected content. It comes only in a binary format, so it can hide DRM decryption implementation details. It can be obtained from a third party or from a Google Chrome installation.

Qt WebEngine on startup looks for the Widevine CDM plugin in well know locations, like default Google Chrome installation directory or Linux distro specific paths. However, plugin location can be also passed with QTWEBENGINE_CHROMIUM_FLAGS 使用 widevine-path .

在 Windows:

set QTWEBENGINE_CHROMIUM_FLAGS=--widevine-path="C:/some path/widevinecdm.dll"
					

在 Linux:

export QTWEBENGINE_CHROMIUM_FLAGS=--widevine-path="/some path/libwidevinecdm.so"
					

在 macOS:

export QTWEBENGINE_CHROMIUM_FLAGS=--widevine-path="/some path/libwidevinecdm.dylib"
					

The video format most commonly used by DRM services, H.264, requires proprietary audio and video codecs. For more information about enabling the codecs, see 音頻和視頻編解碼器 .

This feature can be tested by playing a video in 簡單瀏覽器 or 納米瀏覽器 from castLabs , Swank Motion Pictures, Inc. ,或 Bitmovin Player .

該特徵支持在 Qt 5.7.0 添加。

HTML5 地理位置

Qt WebEngine supports JavaScript Geolocation API with Qt Location as a backend. The application has to explicitly allow the feature by using QWebEnginePage::Geolocation or WebEngineView.Feature .

If Qt Location has been built before Qt WebEngine then this feature can be tested by using 映射 and allowing it to find the current position of the user. Note that on Windows an external GPS receiver must be connected to the application. For more information, see Qt Positioning .

該特徵支持在 Qt 5.5.0 添加。

HTTP/2 協議

Qt WebEngine supports the Chromium implementation of the HTTP/2 協議。

This feature can be tested by opening an HTTP/2 demo, such as the Akamai HTTP/2 Demo , in 簡單瀏覽器 or 納米瀏覽器 .

本機對話框

A web page might request dialogs for the following functions:

  • Entering user credentials for HTTP and proxy authentication
  • Displaying JavaScript alerts, confirmation dialogs, and prompts
  • Picking colors
  • Selecting files
  • Displaying form validation messages

Qt WebEngine provides standard dialogs for these functions. In widget-based applications, the standard dialogs are based on QDialog , whereas in Qt Quick applications, they can be based either on Qt Quick Controls 1 or Qt Quick Controls 2 (since Qt 5.8). The latter are used only on eglfs 平颱。

To explicitly force either dialogs based on Qt Quick Controls 1 or Qt Quick Controls 2, set the QTWEBENGINE_DIALOG_SET environment variable to either QtQuickControls1 or QtQuickControls2 .

Qt WebEngine Widgets dialogs can be customized by reimplementing the QWebEnginePage::chooseFiles (), QWebEnginePage::javaScriptAlert (), QWebEnginePage::javaScriptConfirm (),和 QWebEnginePage::javaScriptPrompt () 函數。

Since Qt 5.8, Qt Quick dialogs can be customized by connecting to the WebEngineView::authenticationDialogRequested (), WebEngineView::javaScriptDialogRequested (), WebEngineView::colorDialogRequested (), WebEngineView::fileDialogRequested (), and WebEngineView::formValidationMessageRequested() signals. For an example, see WebEngine Qt Quick 自定義對話框範例 .

Pepper 插件 API

Qt WebEngine supports loading Pepper Plugin API (PPAPI) plugins if WebEngineSettings::pluginsEnabled or QWebEngineSettings::PluginsEnabled 有設置。

Except for the Adobe Flash Player plugin, the plugins must be loaded manually using the Chromium command line syntax with the --register-pepper-plugins argument. The argument value is a list of entries, separated by commas, that contain the file path and one or several MIME types, separated by semicolons:

<file-path-plugin1>;<mime-type-plugin1>,<file-path-plugin2>;<mime-type1-plugin2>;<mime-type2-plugin2>
					

例如:

--register-pepper-plugins="libppapi_example.so;application/x-ppapi-example"
					

The MIME type is important because it determines which embeds the plugin is used for.

該特徵支持在 Qt 5.6.0 添加。

Pepper Flash Player 插件支持

The Pepper Flash player plugin can be loaded automatically if it is installed in one of the following locations, depending on the platform:

  • Windows
    C:\Windows\SysWOW64\Macromed\Flash\pepflashplayer*.dll
    C:\Windows\System32\Macromed\Flash\pepflashplayer*.dll
    							
  • OS X
    /Library/Internet Plug-Ins/PepperFlashPlayer/PepperFlashPlayer.plugin
    							
  • Linux
    /usr/lib/pepperflashplugin-nonfree/libpepflashplayer.so
    /usr/lib/adobe-flashplugin/libpepflashplayer.so
    /usr/lib/PepperFlash/libpepflashplayer.so
    /usr/lib64/chromium/PepperFlash/libpepflashplayer.so
    							

You can also load the Pepper Flash player from a specific location by using command line arguments:

--ppapi-flash-path=./libpepflashplayer.so
					

By default, the Flash version is set to 11.2.999.999 。可以使用 ppapi-flash-version= argument to set another Flash version in the format major.minor.build.revision :

--ppapi-flash-version=16.0.0.235
					

This feature can be tested in 簡單瀏覽器 or 納米瀏覽器 if the Adobe Flash PPAPI plugin is installed and plugins are enabled in the browser. To test the feature, the https://helpx.adobe.com/flash-player.html page can be opened in the browser.

查看 PDF 文件

Qt WebEngine supports viewing PDF documents by navigating to them. This feature uses the Chromium extensions API and PDF viewer plugin to display the PDF documents. It can be tested in 簡單瀏覽器 or 納米瀏覽器 .

Loading plugins needs to be enabled using QWebEngineSettings::PluginsEnabled or WebEngineSettings::pluginsEnabled in order to use this feature.

This feature can be turned on (default) or off via the QWebEngineSettings::PdfViewerEnabled or WebEngineSettings::pdfViewerEnabled 設置。

該特徵支持在 Qt 5.13.0 添加。

頁麵生命周期 API

Qt WebEngine supports the 頁麵生命周期 API 規範 , a work-in-progress extension to the HTML standard for allowing user agents to reduce their resource consumption by freezing or discarding background pages. The feature is exposed both in the Widgets and QML APIs.

在使用中的 QML API 範例,見 WebEngine 生命周期範例 .

該特徵支持在 Qt 5.14.0 添加。

生命周期狀態概述

每個 WebEngineView item (or QWebEnginePage object) can be in one of three lifecycle states : active, frozen, or discarded. These states, like the sleep states of a CPU, control the resource usage of web views.

The active state is the normal, unrestricted state of a web view. All visible web views are always in the active state, as are all web views that have not yet finished loading. Only invisible, idle web views can be transitioned to other lifecycle states.

The frozen state is a low CPU usage state. In this state, most HTML task sources are suspended (frozen) and, as a result, most DOM event processing and JavaScript execution will also be suspended. The web view must be invisible in order to be frozen as rendering is not possible in this state.

The discarded state is an extreme resource-saving state. In this state, the browsing context of the web view will be discarded and the corresponding renderer subprocess shut down. CPU and memory usage in this state is reduced virtually to zero. On exiting this state the web page will be automatically reloaded. The process of entering and exiting the discarded state is similar to serializing the browsing history of the web view and destroying the view, then creating a new view and restoring its history.

另請參閱 WebEngineView::LifecycleState . The equivalent in the Widgets API is QWebEnginePage::LifecycleState .

The lifecycleState and recommendedState 特性

The lifecycleState 特性為 WebEngineView type is a read-write property that controls the current lifecycle state of the web view. This property is designed to place as few restrictions as possible on what states can be transitioned to. For example, it is allowed to freeze a web view that is currently playing music in the background, stopping the music. In order to implement a less aggressive resource-saving strategy that avoids interrupting user-visible background activity, the recommendedState property must be used.

The recommendedState 特性為 WebEngineView type is a read-only property that calculates a safe limit on the lifecycleState property, taking into account the current activity of the web view. So, in the example of a web view playing music in the background, the recommended state will be Active since a more aggressive state would stop the music. If the application wants to avoid interrupting background activity, then it should avoid putting the web view into a more aggressively resource-saving lifecycle state than what's given by recommendedState .

另請參閱 WebEngineView::lifecycleState and WebEngineView::recommendedState . The equivalents in the Widgets API are QWebEnginePage::lifecycleState and QWebEnginePage::recommendedState .

DOM 擴展

The lifecycleState 特性被連接到 頁麵生命周期 API 規範 ,它指定 2 個新 DOM 事件 freeze and resume ,和添加新的 Document.wasDiscarded 布爾特性。 freeze and resume events are fired when transitioning from the Active Frozen state , and vice-versa. The Document.wasDiscarded property is set to true when transitioning from the Discarded state to the Active 狀態。

Qt WebEngine 支持把網頁打印成 PDF 文件。更多信息,見 QWebEnginePage::printToPdf () 和 WebEngineView.printToPdf .

可以測試此特徵使用 Html2Pdf .

該特徵支持在 Qt 5.7.0 添加。

進程模型

Qt WebEngine uses multiple OS processes to isolate web sites from each other and from the client application, improving security and robustness. The following process models, or ways to divide web sites between OS processes, are supported:

每站點實例進程

This is the default model. Pages from separate sites are put into separate processes and separate visits to the same site are also isolated.

Two web pages are considered as belonging to the same site if they originate from the same registered domain name (for example, wikipedia.org ) and scheme (for example, https ). This is similar to the same-origin policy but subdomains are ignored. For example, both https://en.wikipedia.org/ and https://de.wikipedia.org/ would belong to the same site.

A site instance is a collection of web pages belonging to the same site. When the application explicitly loads a URL into Qt WebEngine (via QWebEnginePage::setUrl , for example), a new site instance is created for the page. However, when the user clicks same-site links on the page, the existing site instance is merely extended with more pages.

For instance, in the 簡單瀏覽器 example, when a user opens two tabs and explicitly enters https://en.wikipedia.org/ into the URL bars, both tabs will have their own separate OS processes (because explicitly entering a URL creates a new site instance). However, when the user then middle-clicks some same-site links to open more tabs, these new tabs will share the same OS process (because user interaction extends the existing site instance).

每站點進程

Pages from separate sites are put into separate processes. Unlike Process per Site Instance, all visits to the same site will share an OS process.

The benefit of this model is reduced memory consumption, because more web pages will share processes. The drawbacks include reduced security, robustness, and responsiveness.

To enable this model, use the command-line argument --process-per-site 。見 使用命令行參數 .

單進程

For debugging purposes only, a single process mode can be enabled using the command-line argument --single-process 。見 使用命令行參數 and Qt WebEngine 調試和剖分析 .

拼寫檢查器

Qt WebEngine supports integrating spellchecking support into HTML forms to enable users to submit spellchecked messages. When the user clicks on an underlined misspelled word, the default context menu displays up to four suggestions. Selecting one will replace the misspelled word.

To be able to check the spelling, the spellchecker needs dictionaries. It supports dictionaries from the Hunspell project , but they have to be compiled into a special binary format. A Hunspell dictionary consists of two files:

  • A .dic file that is a dictionary containing words for the language
  • An .aff file that defines the meaning of special flags in the dictionary

These two files can be converted into the bdic format by using the qwebengine_convert_dict tool that is shipped together with Qt. When the Qt WebEngine spellchecker initializes, it will try to load the bdict dictionaries and to check them for consistency.

QTWEBENGINE_DICTIONARIES_PATH is set, the spellchecker uses the dictionaries in the specified directory without looking anywere else. Otherwise, it uses the qtwebengine_dictionaries directory relative to the executable if it exists. If it does not exist, it will look in QT_INSTALL_PREFIX/qtwebengine_dictionaries .

On macOS, depending on how Qt WebEngine is configured at build time, there are two possibilities how spellchecking data is found:

  • Hunspell dictionaries (default) - .bdic dictionaries are used, just like on other platforms
  • Native dictionaries - the macOS spellchecking APIs are used (which means the results will depend on the installed OS dictionaries)

Thus, in the macOS Hunspell case, Qt WebEngine will look in the qtwebengine_dictionaries subdirectory located inside the application bundle 資源 directory, and also in the 資源 directory located inside the Qt framework bundle.

To summarize, in case of Hunspell usage, the following paths are considered:

Spellchecking is disabled by default and can be enabled per profile by using the QWebEngineProfile::setSpellCheckEnabled () method in widget-based applications and the WebEngineProfile.spellCheckEnabled property in Qt Quick applications.

The current language used for spellchecking is defined per profile, and can be set using the QWebEngineProfile::setSpellCheckLanguages () method or the WebEngineProfile.spellCheckLanguages 特性。

可以測試此特徵通過構建和運行 拼寫檢查器範例 .

該特徵支持在 Qt 5.8.0 添加。

觸摸

Qt WebEngine 支持導航和交互網頁的觸摸設備。

Applications can prohibit the use of touch events in the following ways:

  • Passing the flag --touch-events=disabled on the command line will disable touch event support in JavaScript API (meaning ontouchstart and related handlers will not be present in the document.window object). Touch events will still be delivered to web pages.
  • Installing an event filter object using QObject::installEventFilter WebEngine view focus proxy object, and filtering out all touch events.

查看源

Qt WebEngine 支持查看網頁的 HTML 源。

This feature can be used from custom menus or assigned to custom events. For more information, see WebEngineView::WebAction ,和 QWebEnginePage::WebAction .

This feature can be tested by opening a web page in 簡單瀏覽器 or 納米瀏覽器 , and then selecting Page Source in the context menu. The Page Source context menu entry opens the source view in a new tab.

For opening the source view in the current tab, URLs with view-source URI scheme are also supported. For example, you can type the following URL to the URL bar to view the HTML source of the qt.io web page:

view-source:https://www.qt.io/
					

Auto-completion of incomplete URLs with view-source URI scheme makes the usage of this feature more comfortable. For example, the following incomplete URL also loads the source view of the qt.io web page:

view-source:qt.io
					

該特徵支持在 Qt 5.8.0 添加。

WebRTC

WebRTC 憑藉簡單 API 為瀏覽器提供 RTC (實時通信) 能力。更多信息,見 WebEngineView.Feature and QWebEnginePage::Feature .

This feature can be tested by setting up a webcam or microphone and then opening https://test.webrtc.org/ in 簡單瀏覽器 or 納米瀏覽器 .

Web 通知

Qt WebEngine 支持 JavaScript Web 通知 API 。應用程序必須明確允許特徵通過使用 QWebEnginePage::Notifications or WebEngineView.Notifications .

該特徵支持在 Qt 5.13.0 添加。