[Kde-bindings] questions about signals and slots in qruby4

Richard Dale rdale at foton.es
Mon Aug 4 18:06:01 UTC 2008


On Sunday 03 August 2008 14:23:10 Stefano Crocco wrote:
> I have a few questions about signals, slots and connections in qtruby4:
>
> * 'connect' if called with an explicit receiver wants a block and raises an
> error if called in the non-block form. For example, the following code
> works:
>
> require 'Qt4'
>
> app =  Qt::Application.new []
> btn = Qt::PushButton.new "Sender"
> label = Qt::Label.new "Receiver"
> btn.connect(SIGNAL('clicked()')){label.clear}
> btn.show
> label.show
> app.exec
>
> but if I replace the connect line with:
>
> btn.connect(SIGNAL('clicked()'), label, SLOT('clear()'))
>
> I get the following error:
>
> ./prova.rb:9:in `method_missing': super: no superclass method `connect'
> (NoMethodError)
>         from ./prova.rb:9:in `connect'
>         from ./prova.rb:9
>
> (At first, I thought the error might have had been caused by calling
> connect from the top-level object, given that it spoke about a missing
> superclass method, but trying it inside an instance method of a
> Qt::Widget-derived class gave the same result). Is this wanted, or is it a
> bug? If it is wanted, is there another way to make a connection outside
> from an instance method of a Qt::Object-derived object? In C++ (I think)
> you can write:
>
> QObject::connect(...)
>
> In ruby this doesn't work. Is there another way?
You need to call connect on the label, not on the button like this:

label.connect(btn, SIGNAL('clicked()'), SLOT('clear()'))

And C++ is exactly the same.

>
> * It seems to be impossible to reopen predefined Qt/KDE classes to add
> signals and slots to them, even if it is possible to add common methods.
> For example, the following works:
>
> require 'Qt4'
>
> class Qt::PushButton
>   def custom_method
>     puts "custom method"
>   end
> end
>
> app = Qt::Application.new []
> w = Qt::PushButton.new
> w.custom_method
>
> Instead, if I try to add a signal, I get an error when I try to connect to
> it:
>
> require 'Qt4'
>
> class Qt::PushButton
>   signals 'custom_signal()'
> end
>
> app = Qt::Application.new []
> w = Qt::PushButton.new
> w.connect(SIGNAL('custom_signal()')){puts "custom_signal"}
>
> The error message is:
>
> /usr/lib/ruby/site_ruby/1.8/Qt/qtruby4.rb:2706:in `getMetaObject':
> undefined method `name' for nil:NilClass (NoMethodError)
>         from /usr/lib/ruby/site_ruby/1.8/Qt/qtruby4.rb:2707:in
> `getMetaObject' from /usr/lib/ruby/site_ruby/1.8/Qt/qtruby4.rb:2740:in
> `metaObject' from /usr/lib/ruby/site_ruby/1.8/Qt/qtruby4.rb:2740:in
> `method_missing'
>         from /usr/lib/ruby/site_ruby/1.8/Qt/qtruby4.rb:2740:in `connect'
>         from /usr/lib/ruby/site_ruby/1.8/Qt/qtruby4.rb:2740:in
> `signal_connect'
>         from prova.rb:9:in `connect'
>         from prova.rb:9
>
> The same seems to happen for slots. Again, is this simply a bug, or it
> simply can't be done?
It used to be possible, but after I fixed some other bugs the new 
implementation of the way slots/signals worked with QMetaObjects meant that 
it is now no longer possible. So I wouldn't say it is a bug, but that former 
functionality is no longer present. It may be possible to get it working 
again, but I think adding slots as blocks is a better and more rubyish way to 
achieve the same thing.

-- Richard



More information about the Kde-bindings mailing list