2011-10-03 59 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 

可能是垃圾收集器去破壞哈希對象和檢索對象失敗的嘗試......