Qt WebChannel 独立范例

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

运行范例

要运行范例从 Qt Creator ,打开 欢迎 模式,然后选择范例从 范例 。更多信息,拜访 构建和运行范例 .

与远程客户端通信

C++ 应用程序设置 QWebChannel 实例和发布 Core object over it. For the remote client side, index.html is opened. Both show a dialog with the list of received messages and an input box to send messages to the other end.

Core 发射 Core::sendText() 信号,当用户发送消息时。信号被自动传播到 HTML 客户机。当用户在 HTML 端键入消息时, Core::receiveText() 被调用。

All communication between the HTML client and the C++ server is done over a WebSocket. The C++ side instantiates a QWebSocketServer 并包裹传入 QWebSocket 连接在 QWebChannelAbstractTransport 对象。然后,把这些对象连接到 QWebChannel 实例。

文件:

另请参阅 Qt WebChannel JavaScript API .