[Kde-bindings] QtRuby slots/signals working

Richard Dale Richard_Dale at tipitina.demon.co.uk
Thu Jul 24 08:20:27 UTC 2003


I've just got tutorial t7 working in qtruby, and I'm really pleased. Nearly 
everything has to work in order to be able to connect a C++ signal to a ruby 
signal, and then to a ruby slot. Constructing metaObjects on the fly, 
overriding qt_invoke and qt_emit, dynamically adding signal methods.. phew!

Although while fishing around in the qobject.cpp code to see what's going on, 
I did notice this:
...
			|| ( QUType::isEqual( sm->method->parameters[si-1].type, 
&static_QUType_varptr )
			     && QUType::isEqual( rm->method->parameters[ri-1].type, 
&static_QUType_ptr ) ) )
			continue; // varptr got introduced in 3.1 and is binary compatible with ptr
...

So I don't know if the new 'static_QUType_varptr' will mean any changes to the 
slots/signals perl or ruby runtime code.

Here's what lcdrange.rb and t7.rb have come out looking like (note the signal 
and slot declarations are just lists of strings):

#!/usr/bin/ruby -w
require 'Qt'

class LCDRange < Qt::VBox
	signals "valueChanged(int)"
	slots "setValue(int)"

	def initialize(grid)
		super
		lcd = Qt::LCDNumber.new(2, self, "lcd")
	       @slider = Qt::Slider.new(VBox.Horizontal, self, "slider")
	       @slider.setRange(0, 99)
	       @slider.setValue(0)
		connect(@slider, SIGNAL('valueChanged(int)'), lcd, SLOT('display(int)'))
		connect(@slider, SIGNAL('valueChanged(int)'), SIGNAL('valueChanged(int)'))
	end

	def value()
    	  @slider.value()
	end

	def setValue( value )
    	  @slider.setValue( value )
	end
end

#!/usr/bin/ruby -w
require 'Qt'
require "lcdrange.rb"

class MyWidget < Qt::VBox

def initialize()
    quit = Qt::PushButton.new("Quit", self, "quit")
    quit.setFont(Qt::Font.new("Times", 18, Qt::Font.Bold))
    
	connect(quit, SIGNAL('clicked()'), $qapp, SLOT('quit()'))
	grid = Qt::Grid.new( 4, self )

	previous = nil	
	for c in 0..3
		for r in 0..3
			lr = LCDRange.new(grid)
			if previous != nil
				connect( lr, SIGNAL('valueChanged(int)'),
						previous, SLOT('setValue(int)') )
			end
			previous = lr
		end
	end
end

end    

a = Qt::Application.new(ARGV)

w = MyWidget.new
a.setMainWidget(w)
w.show
a.exec

On Wednesday 23 July 2003 00:11, Germain Garand wrote:
> Le Lundi 21 Juillet 2003 20:30, Richard Dale a écrit :
> > Ah I see! The trouble is the pseudo-code looks a lot like the actual
> > perl, and I'm not much of a perl expert at all, and didn't understand
> > what it means. But it shouldn't be too difficult to sort out - one of my
> > ruby books 'The Ruby Way' has a really good section called 'From Perl to
> > Ruby' - I think it's just a matter of going through all the perl type
> > mappings and seeing how they compare with ruby types.
>
> Just to clarify further the confusing part : what was meant by the original
> ref() test in Perl code is simply  "is the argument a complex/composite
> object, like an array or a hash" ...
> Perl has no way to pass such an object directly in the @_ array because it
> would be flattened, so Perl coders use references to hashes or arrays (e.g
> foo(\@bar) )
> This has nothing to do with C++ refs, which are handled by the handlers.cpp
Got it - the code looks like this now and works fine:

			for arg in args
				if arg == nil or isObject(arg)
					method << "#"
				elsif arg.kind_of? Array or arg.kind_of? Hash
					method << "?"
				else
					method << "$"
				end
			end

But there is a problem with passing references to primitive C++ types from 
ruby, the 'int&' marshaller does this:

		int i = SvIV(sv);
		m->item().s_voidp = &i;
		m->next();
		sv_setiv_mg(sv, (IV)i);

..and there is no equivalent of that in ruby (although there is something 
similar called a 'hooked variable'). I think any primitive type reference 
arguments would need to be updated from the current C++ argument values after 
the Qt C++ method call has returned.

> Otherwise, good news: I eventually "solved" the linux dl problem by simply
> doing LD_PRELOAD=<path>/Qt.so the_prog.rb
> Bliss! :-)
Good! I'm just running everything where it's built at the moment.

-- Richard

-------------- next part --------------
A non-text attachment was scrubbed...
Name: qtruby_0.6.tar.gz
Type: application/x-tgz
Size: 35027 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-bindings/attachments/20030724/3e322032/attachment.bin>


More information about the Kde-bindings mailing list