QtRuby : constructor optional parameter

A

Arturo Bonechi

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
 
S

Stefano Crocco

|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

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
 
A

Arturo Bonechi

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

"Segmentation fault" with QtRuby 2
QtRuby - Error 0
Qt problems 3
Qt. Dialog and main windows location 0
QtRuby Questions: Why does 2
[ANN] QtRuby 1.4.7 12
install_qtruby_on_mac 7
Where is the magic? 3

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top