Ruby QWebChannel:在Ruby代码中利用QWebChannel与C++ QT5进行通信
Ruby QWebChannel,是对于QT官方的 qwebchannel.js 进行移植得到的Ruby版本。目的是让Ruby代码也能像Javascript代码那样利用 QWebChannel 与C++ QT5代码进行自然的通信。
当前版本号:2019.1.5。
2019.1.5版本,具有以下功能特性:
1.解析C++侧的接口描述消息,动态生成对应的Ruby对象,供调用接口,及注册信号回调代码。
使用gem即可安装Ruby QWebChannel库:
gem install QWebChannel
以下示例具体展示了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
点击以下链接在线查看文档:
https://www.rubydoc.info/gems/QWebChannel/
或扫描以下二维码在手机上查看文档:
HxLauncher: Launch Android applications by voice commands