Qt WebChannel 独立示例

服务器与浏览器中运行的远程客户端之间的简单聊天。

Standalone演示了如何使用QWebChannel C++ API 与外部客户端通信。这是一个 C++ 应用程序与在默认浏览器中运行的远程 HTML 客户端之间的简单聊天。

运行示例

要从 Qt Creator,打开Welcome 模式,然后从Examples 中选择示例。更多信息,请参阅Qt Creator: 教程:构建并运行

与远程客户端通信

C++ 应用程序设置了一个QWebChannel 实例,并在该实例上发布了一个Core 对象。远程客户端则打开index.html 文件。两者都会显示一个对话框,其中包含接收到的信息列表和一个向另一端发送信息的输入框。

当用户发送信息时,Core 会发出Core::sendText() 信号。该信号会自动传播到 HTML 客户端。当用户在 HTML 端输入信息时,Core::receiveText() 就会被调用。

HTML 客户端与 C++ 服务器之间的所有通信都是通过 WebSocket 完成的。C++ 端实例化QWebSocketServer ,并将传入的QWebSocket 连接封装到QWebChannelAbstractTransport 对象中。然后将这些对象连接到QWebChannel 实例。

示例项目 @ code.qt.io

另请参阅 Qt WebChannel JavaScript API

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