本页内容

Qt WebSockets

WebSocket 是一种基于 Web 的协议,旨在在客户端应用程序与远程主机之间建立双向交互通信会话。如果初始握手成功,该协议可使这两个实体之间进行数据收发。

Qt WebSockets 模块提供了C++和Qml接口,使Qt应用程序能够充当处理WebSocket请求的服务器、接收来自服务器数据的客户端,或同时兼具这两种角色。

使用该模块

QML API

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

import QtWebSockets

C++ API

要使用 Qt 模块的 C++ API,需要链接该模块库,既可以直接链接,也可以通过其他依赖项间接链接。包括CMake和qmake 在内的多种构建工具都对此提供了专门支持。

使用 CMake 构建

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

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

更多详细信息,请参阅“使用 CMake 构建”概述。

使用 qmake 进行构建

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

QT += websockets

示例

参考

符合性

许可证

Qt WebSockets 可通过The Qt Company 提供的商业许可证使用。此外,它也可在自由软件许可证下使用。自 Qt 5.4 起,这些自由软件许可证包括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.