Josh Stratton wrote:
> Hey, I can just change it to QWidget, and I get a connection function
> now.  This is wonderful!  One other question, though.  Is there a way
> to create custom slots in just ruby? 

You should really ask these questions at jruby resource - they are
related to jruby/java integration and you'll get better help there.

Answering you question - JRuby generates very crappy code from
reflection point of view. It's impossible to reference on jruby symbols
using reflection unless those forced by something - like java interface
impl.

So my recipe looks like this:

Action.java:
public interface Action {
    void apply();
}

signal.rb:

require "java"

include_class Java::Action
include_class Java::com.trolltech.qt.gui.QWidget
include_class Java::com.trolltech.qt.gui.QPushButton
include_class Java::com.trolltech.qt.gui.QApplication

class RubyAction
     include Action
     def initialize(f)
        @f = f
     end
     def apply
        @f.call
     end
end

class QWidget
    def on(sym, &block)
        self.getClass.fields.select {|f| f.name == sym.id2name
}.first.get(self).connect(RubyAction.new(block), "apply()")
    end
end

QApplication.initialize([].to_java(:string))
btn = QPushButton.new("quit")
btn.resize(100,100)

btn.on :clicked do
     QApplication.quit
end

btn.show
QApplication.exec



As you can see it uses better looking syntax with code blocks than
reflective slots approach.

-- 
Best Regards,
Vladimir Kirichenko
_______________________________________________
Qt-jambi-interest mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest

Reply via email to