# QtRuby : constructor optional parameter

**URL:** <https://www.ruby-forum.com/t/qtruby-constructor-optional-parameter/193373>\
**Category:** Ruby\
**Created:** [August 16, 2010, 7:33pm UTC](https://www.ruby-forum.com/t/qtruby-constructor-optional-parameter/193373 "2010-08-16T19:33:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonechi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bonechi](https://www.ruby-forum.com/u/bonechi)\
**Post date:** [August 16, 2010, 7:33pm UTC](https://www.ruby-forum.com/t/qtruby-constructor-optional-parameter/193373/1 "2010-08-16T19:33:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bonechi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bonechi](https://www.ruby-forum.com/u/bonechi)\
**Post date:** [August 16, 2010, 8:24pm UTC](https://www.ruby-forum.com/t/qtruby-constructor-optional-parameter/193373/2 "2010-08-16T20:24:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bonechi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bonechi](https://www.ruby-forum.com/u/bonechi)\
**Post date:** [August 16, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/qtruby-constructor-optional-parameter/193373/3 "2010-08-16T20:50:09Z")

</div>

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.
