QtRuby : constructor optional parameter

Hello everyone!

I would like to know how to manage (with QtRuby) the optional parameter
from the constructor of a lot of classes (in Qt C++).

I join a quick example.

Thank you!

Here is the code:

require “Qt4”

class Tree < Qt::TreeWidget
def initialize parent # = 0 ??? (or something like that)
# because I want this parameter to be
# optional like in the parent class
super parent
end
end

class Window < Qt::MainWindow
def initialize
super

# I have to use self whereas I want NO parameter
setCentralWidget Tree.new self

end
end

if FILE == $0
a = Qt::Application.new ARGV

g = Window.new
g.show

a.exec
end

On Monday 16 August 2010, Arturo Bonechi wrote:

|
|class Window < Qt::MainWindow
|
| g = Window.new
| g.show
|
| a.exec
|end

Use nil as default value. Generally speaking, everywhere the C++ takes a
QObject* or QWidget* parameter which can be 0, with qtruby you can pass
nil.
So, the initialize method of your Tree class would be:

class Tree < Qt::TreeWidget

def initialize parent = nil
super
end

end

Note that you don’t need to pass any argument to super: calling it
without
arguments automatically passes it all the arguments passed to the
overridden
method.

I hope this helps

Stefano

Thank you for the quick reply!

Use nil as default value. Generally speaking, everywhere the C++ takes a
QObject* or QWidget* parameter which can be 0, with qtruby you can pass
nil.

Awright! You solved my problem :^)

Note that you don’t need to pass any argument to super: calling it
without
arguments automatically passes it all the arguments passed to the
overridden
method.

Thanks for the tip, I’m new to Ruby and I didn’t know that!

I hope this helps

Indeed, it helps ;^)

Thanks again.