Qt WebView
Qt WebView 可让您在 QML 应用程序中显示网页内容。为避免包含完整的网络浏览器栈, 在适当的地方使用本地 API。Qt WebView
这在Android和iOS 等移动平台上非常有用。在 iOS 上,政策规定所有网页内容都要使用操作系统的网页视图来显示。
在 Windows 和 Linux 上,Qt WebView 依靠 Qt WebEngine模块来呈现内容。
在 macOS 上,系统网页视图的使用方式与 iOS 相同。
前提条件
为使 Qt XMLWebView 模块在所有平台上正常运行,有必要在创建模块前调用 QtWebView::initialize()
在创建 QGuiApplication
实例和窗口的QPlatformOpenGLContext
创建之前调用。
使用模块
QML API
模块的 QML 类型可通过QtWebView
import 使用。要使用这些类型,请在 .qml 文件中添加以下导入语句:
import QtWebView
C++ API
使用 Qt 模块的 C++ API 需要直接或通过其他依赖关系与模块库链接。一些编译工具对此提供了专门支持,包括CMake和qmake。
使用 CMake 构建
使用find_package()
命令在Qt6
软件包中找到所需的模块组件:
find_package(Qt6 REQUIRED COMPONENTS WebView) target_link_libraries(mytarget PRIVATE Qt6::WebView)
更多详情,请参阅使用 CMake 构建概述。
使用 qmake 构建
要配置模块以便用 qmake 构建,可在项目的 .pro 文件中添加模块作为QT
变量的值:
QT += webview
限制
由于平台限制,不支持将WebView 与其他 QML 组件重叠。这样做会产生不可预知的结果,不同平台的结果可能不同。应用程序也不能依赖WebView 中的事件传播到 Qt XML 事件交付系统。例如,不能在WebView 上 "覆盖 "一个不可见的项目来处理某些事件,或处理WebView 在父项目中没有处理的事件。
示例
请查看Qt WebView 示例,了解如何在应用程序中使用 API。
参考资料
许可证
Qt WebView 该 API 可在The Qt Company 的商业许可下使用。此外,它还可以在GNU Lesser General Public License 第 3 版或GNU General Public License 第 2 版下使用。更多详情,请参阅Qt Licensing。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.