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
可通过QtWebChannel 导入使用该模块的 QML 类型。要使用这些类型,请在您的 .qml 文件中添加以下导入语句:
import QtWebChannelC++ API
要使用 Qt 模块的 C++ API,需要将模块库链接到项目中,既可以直接链接,也可以通过其他依赖项间接链接。包括CMake和qmake 在内的多种构建工具都为此提供了专门的支持。
使用 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 由The Qt Company 提供商业许可。此外,它还提供自由软件许可。这些自由软件许可包括GNU 弱通用公共许可证第 3 版或 GNU 通用公共许可证第 2 版。更多详细信息,请参阅Qt 许可。
© 2026 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.