Qt problems (original) (raw)

On Saturday 13 June 2009, Damjan R. wrote:

|
| @statusbar.setObjectName(‘statusbar’)
|
|I am learning qt4 with ruby and have got to a problem that is driving me
|crazy. Acording to what I was able to find on Internet
|
| central_widget = layout1
|
|should paint a label and a box in the window. But it doesn’t. It doesn’t
|show anything.

I don’t know where you found this, but it’s clearly incorrect. The C++
method
setCentralWidget (which in ruby can be called with the three names
setCentralWidget, set_central_widget and central_widget=) takes a widget
as
argument. layout1 is a Qt::Layout, not a Qt::Widget, so you can’t pass
it to
setCentralWidget.

|
|Original c++ documentation points to setCentralWidget( layout1 ) method
|but it doesn’t exist in ruby Qt4.

As I explained above, the setCentralWidget does exist, but it requires a
Qt::Widget as argument. Here you’ve come across one of the peculiarities
of
QtRuby: most ruby libraries either don’t check methods arguments types
or
raise ArgumentError or TypeError if one argument is of the wrong type.
QtRuby,
instead, checks the arguments, but raises NoMethodError if the argument
is of
the wrong type. Since you’re calling setCentralWidget with an argument
of the
wrong type (Qt::Layout instead of Qt::Widget) you get a NoMethodError.

The reason why you don’t get the error when calling central_widget= is
that
here you’re not actually calling the method, but creating a local
variable
called central_widget. This isn’t a qtruby issue, but a quirk of the
ruby
syntax: whenever ruby sees something which can be a local variable
followed by
an equal sign, it interprets it as a local variable assignement. This
means
that to have ruby understand you’re calling the central_widget= method
of the
main window, you have to call it using the dot notation:

self.central_widget = widget

(this is an issue only when calling a method ending in = on self: if
you’re
calling it on another object, you naturally do it using the dot
notation, so
there can be no ambiguity).

Here’s a working version of your code:

require ‘Qt4’
class MainWindow < Qt::MainWindow
def initialize
super
self.window_title = ‘Hello QtRuby v1.0’
resize(200, 100)
cw = Qt::Widget.new self
self.central_widget = cw

button = Qt::PushButton.new('Quit', cw) do
  connect(SIGNAL :clicked) { Qt::Application.instance.quit }
end

label = Qt::Label.new(Qt::Object.tr('<big>Hello Qt in the Ruby

way!’), cw)

cw.layout = Qt::VBoxLayout.new cw do
  add_widget(label, 0, Qt::AlignCenter)
  add_widget(button, 0, Qt::AlignRight)
end

status_bar.show_message Qt::Object.tr('Some text')

end
end
if $0 == FILE
a = Qt::Application.new(ARGV)
w = MainWindow.new
w.show
a.exec
end

I made several changes:

|It looks like that Qt with ruby is not used a lot.

I’m not so sure about this.

I hope this helps

Stefano