开发者

QT Ruby bindings signal-slot with hash param

开发者 https://www.devze.com 2023-04-10 21:10 出处:网络
Im using qtbindings for Ruby (https://github.com/ryanmelt/qtbindings) and i would emit a signal with an hash param...

Im using qtbindings for Ruby (https://github.com/ryanmelt/qtbindings) and i would emit a signal with an hash param...

Something like this:

require 'Qt'

class Foo < Qt::Object

  signals 'my_signal(Hash)'
  slots 'my_slot(Hash)'

  def initialize(parent = nil)
    super(parent)
    connect(self, SIGNAL('my_signal(Hash)'), self, SLOT('my_slot(Hash)'))
  end

  def emit_my_signal
    emit my_signal({:foo => :bar})
  end

  def my_slot(hash)
    puts hash.inspect
  end
end

o = Foo.new
开发者_如何学Co.emit_my_signal

If I run this script I get the error: Cannot handle 'Hash' as slot argument (ArgumentError). If I use int instead of Hash everything is fine.

There is a way to do this?? How?

Thanks.


Ok, I've found a solution: pass the string of the Ruby Object ID... Not use the ID as Fixnum because Ruby Fixnum objects may be up to 62 bits, but C ints are 32 bit. When you get the object id you can try to retrieve the object with ObjectSpace._id2ref(object_id_as_string.to_i).

My solution code:

require 'Qt'

class Foo < Qt::Object

  signals 'my_signal(const QString&)'
  slots 'my_slot(const QString&)'

  def initialize(parent = nil)
    super(parent)
    connect(self, SIGNAL('my_signal(const QString&)'), self, SLOT('my_slot(const QString&)'))
  end

  def emit_my_signal
    emit my_signal({:foo => :bar}.object_id.to_s)
  end

  def my_slot(object_id)
    hash = ObjectSpace._id2ref(object_id.to_i)
    puts hash.inspect
  end
end

o = Foo.new
o.emit_my_signal

May be that the garbage collector go to destroy the hash object and the attempt to retrieve the object fail...

0

精彩评论

暂无评论...
验证码 换一张
取 消