2011-10-03 26 views
2

im使用qtbindings为红宝石(https://github.com/ryanmelt/qtbindings) ,我将发射用散列PARAM的信号...QT Ruby绑定信号时隙与散列PARAM

喜欢的东西这样的:

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 
o.emit_my_signal 

如果我运行此脚本,我得到的错误:Cannot handle 'Hash' as slot argument (ArgumentError). 如果我使用的int代替Hash一切都很好。

有一种方法可以做到这一点??怎么样?

谢谢。

+1

我检查还HTTP:// techbase.kde.org/Development/Languages/Ruby#Emitting_Ruby_Classes但似乎不适合我... – Pioz

+0

理查德戴尔建议我使用const QMap &'。 – Pioz

回答

1

好的,我找到了一个解决方案: 传递Ruby对象ID的字符串...不使用ID作为Fixnum,因为Ruby Fixnum对象可能高达62位,但C ints是32位。 当您获取对象ID时,您可以尝试使用ObjectSpace._id2ref(object_id_as_string.to_i)检索对象。

我的解决办法代码:

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 

可能是垃圾收集器去破坏哈希对象和检索对象失败的尝试......