Qt WebChannel

Qt WebChannel 可实现服务器(QML/C++ 应用程序)与客户端(HTML/JavaScript 或 QML 应用程序)之间的点对点通信。开箱即用的 Qt WebEngine.此外,它还能在所有支持WebSockets 的浏览器上运行,使Qt WebChannel 客户端能在任何 JavaScript 环境(包括 QML)中运行。这需要基于Qt WebSockets 实现自定义传输。

该模块提供了一个 JavaScript 库,用于将 C++ 和 QML 应用程序与 HTML/JavaScript 和 QML 客户端无缝集成。客户端必须使用 JavaScript 库访问主机应用程序发布的序列化 QObject。

使用模块

QML API

模块的 QML 类型可通过QtWebChannel 导入使用。要使用这些类型,请在 .qml 文件中添加以下导入语句:

import QtWebChannel

C++ API

使用 Qt 模块的 C++ API 需要直接或通过其他依赖关系与模块库链接。一些编译工具对此提供了专门支持,包括CMakeqmake

使用 CMake 构建

使用find_package() 命令在Qt6 软件包中找到所需的模块组件:

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

更多详情,请参阅使用 CMake 构建概述。

使用 qmake 构建

要配置使用 qmake 构建的模块,请在项目的 .pro 文件中添加模块作为QT 变量的值:

QT += webchannel

示例

参考

许可证

Qt WebChannel JavaScript API 可通过The Qt Company 的商业许可获得。此外,它还以自由软件许可证的形式提供。这些自由软件许可证包括GNU Lesser General Public License 第 3 版GNU General Public License 第 2 版。更多详情,请参阅Qt 许可

© 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.