StupidBeauty
Read times:185Posted at:Sat Jan 5 10:12:42 2019 - no title specified

Ruby QWebChannel在Ruby代码中利用QWebChannel与C++ QT5进行通信

内容目录

1 概述

2 变更说明

3 下载及安装

4 使用示例

5 文档

1 概述

Ruby QWebChannel,是对于QT官方的 qwebchannel.js 进行移植得到的Ruby版本。目的是让Ruby代码也能像Javascript代码那样利用 QWebChannel 与C++ QT5代码进行自然的通信。

当前版本号:2019.1.5。

2 变更说明

2019.1.5版本,具有以下功能特性:

  1. 1.解析C++侧的接口描述消息,动态生成对应的Ruby对象,供调用接口,及注册信号回调代码

3 下载及安装

使用gem即可安装Ruby QWebChannel

gem install QWebChannel

4 使用示例

以下示例具体展示了Ruby QWebChannel的用法。此示例可与QT官方文档中的Qt WebChannel Standalone Example示例配合使用。

点击以下链接下载示例源代码:

https://bitbucket.org/hxcan/rubyqwebchannel/downloads/StandaloneExample.tar.gz

或扫描以下二维码以下载到手机,然后传送到电脑:

也可以对着下面的代码手动抄写一份:

#!/usr/bin/env ruby

require 'websocket-eventmachine-client'

require 'QWebChannel'

$core ={}

$showReceivedMessageFromServer = Proc .new do |messageFromServer|

puts "Received message from server: #{messageFromServer} "

end #$showReceivedMessageFromServer=Proc.new do |messageFromServer|

EM .run do

    ws = WebSocket :: EventMachine :: Client .connect(:uri => 'ws://localhost:12345')

    ws.onopen do

        qWebChannel = QWebChannel .new(ws) do |channel|

$core =channel.objects [ 'core' ]

puts "Connected to WebChannel, ready to send/receive messages!"

puts "Sending message 'hello from client' to the server."

$core .receiveText('hello from client') #Call the "receiveText" slot on the server side.

$core [ 'sendText' ] .connect($showReceivedMessageFromServer) #Connect to the "sendText" signal on the server side.

end #qWebChannel = QWebChannel.new(ws) do |channel|

end #ws.onopen do

    ws.onclose do |code, reason|

puts "Disconnected with status code: #{code} "

EM .stop

end

end #EM.run do

5 文档

点击以下链接在线查看文档:

https://www.rubydoc.info/gems/QWebChannel/

或扫描以下二维码在手机上查看文档:

Your opinions
Your name:Email:Website url:Opinion content:
支付宝,我叫你一声你敢立即启动吗?

灵桌面:支付宝,我叫你一声你敢立即启动吗?