Qt WebView

Qt WebView lets you display web content inside a QML application. To avoid including a full web browser stack, Qt WebView uses native APIs where appropriate.

This is useful on mobile platforms, such as Android and iOS . On iOS, policies dictate that all web content is displayed using the operating system's web view.

On Windows and Linux, Qt WebView depends on the Qt WebEngine 模塊渲染內容。

On macOS, the system web view is used in the same manner as iOS.

先決條件

要使 Qt WebView 模塊函數正確跨所有平颱,有必要調用 QtWebView::initialize() 先於創建 QGuiApplication 實例和先於窗口的 QPlatformOpenGLContext 被創建。

使用模塊

QML API

模塊的 QML 類型是可用的透過 QtWebView 導入。要使用類型,添加以下 import 語句到 .qml 文件:

import QtWebView
					

C++ API

使用 Qt 模塊的 C++ API 要求直接 (或透過其它依賴) 鏈接到模塊庫。幾個構建工具對此有專用支持,包括 CMake and qmake .

構建采用 CMake

使用 find_package() 命令定位所需模塊組件在 Qt6 包:

find_package(Qt6 REQUIRED COMPONENTS WebView)
target_link_libraries(mytarget PRIVATE Qt6::WebView)
					

更多細節,見 構建采用 CMake 概述。

采用 qmake 構建

要配置采用 qmake 構建模塊,添加模塊作為值為 QT 變量在工程的 .pro 文件:

QT += webview
					

局限性

由於平颱的限製,重疊 WebView with other QML components is not supported. Doing this will have unpredictable results, which may differ from platform to platform. Applications can also not rely on events in the WebView to propagate into the Qt event delivery system. E.g. it is not possible to "overlay" an invisible item on top of the WebView to handle certain events, or to handle events that the WebView doesn't process in a parent item.

範例

Look at the Qt WebView 範例 瞭解如何在應用程序中使用 API 的演示。

參考

許可

Qt WebView 在商業許可下是可用的來自 Qt 公司 。此外,它是可用的根據 GNU LGPL (次一般公共許可) 第 3 版 ,或 GNU GPL (一般公共許可) 第 2 版 。見 Qt 許可 進一步瞭解細節。