[Kde-bindings] Signal-slot with hash param
Stefano Crocco
stefano.crocco at alice.it
Mon Oct 3 05:48:55 UTC 2011
On Monday 03 October 2011 03:21:58 Enrico Pilotto wrote:
> Hi,
> 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
> o.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.
Unfortunately, QtRuby only supports signals and slots having C++ types as
arguments. This means you can use ruby booleans, integer, floats, strings and
all Qt classes as arguments, because they correspond to some C++ class (and
even in this case, note that you have to use the C++ name of the type in the
call to connect, not the ruby name. For example, you need to use QString
instead of the ruby String class name). Hashes don't have a corresponding
class in C++ (actually, Qt does have a QHash class, but I don't think it's
supported).
According to the QtRuby site [1], you have two possible way of emitting (and
connecting to) ruby classes. One is to use a signal which takes as argument
the object_id of the object (which is an int) and retrieve the object itself
in the slot using ObjectSpace._id2ref. I find this quite ugly, however. The
other option is to have a signal using a Qt::Variant as argument and wrap your
object in it using Qt::Variant.from_value, then unwrap it in your slot using
Qt::Variant#value.
I hope this helps
Stefano
[1] http://techbase.kde.org/Development/Languages/Ruby#Emitting_Ruby_Classes
More information about the Kde-bindings
mailing list