[Kde-bindings] QtRuby section in my book
Richard Dale
Richard_Dale at tipitina.demon.co.uk
Thu Apr 6 09:26:37 UTC 2006
On Wednesday 05 April 2006 20:47, Jason D. Clinton wrote:
> Hello list;
>
> I've been lurking for quite some time but I finally have something to
> say. I have a section in my upcoming book on using QtRuby for RAD. The
> book is a 'quick reference/pocket reference' style of book called Ruby
> Phrasebook (it's up on Amazon.com). Anyway, it covers Qt4 since that's
> the first release that's GPL for all platforms. Below I have a "Hello
> World" example that I wanted to run by you guys: see any major problems
> with my approach WRT Qt4? There's no documentation out there, yet ...
Another book is good news!
>
> require 'Qt'
>
> app = Qt::Application.new ARGV
>
> class MyDialog < Qt::Widget
> slots 'button_clicked()'
>
> def initialize(parent=nil)
> super(parent)
>
> @vgroup = Qt::VBoxLayout.new
> @button = Qt::PushButton.new "Hello World!"
> @edit = Qt::LineEdit.new "Hello?"
>
> @vgroup.addWidget @button
> @vgroup.addWidget @edit
>
> connect @button, SIGNAL('clicked()'), self,
> SLOT('button_clicked()')
>
> self.setLayout @vgroup
> end
>
> def button_clicked()
> @edit.text = 'Hello! Hello!'
> end
> end
>
> window = MyDialog.new
> window.show
>
> app.exec
It looks fine to me. I prefer to pass widgets in a block to a layout
constructor like this:
@vgroup = Qt::VBoxLayout.new do |v|
@button = Qt::PushButton.new "Hello World!"
@edit = Qt::LineEdit.new "Hello?"
v.addWidget @button
v.addWidget @edit
end
And, rather than:
self.setLayout @vgroup
Either:
self.layout = @vgroup
Or:
setLayout @vgroup
Note that you don't need to keep widgets in instance variables if they aren't
needed once they are installed in a layout. They won't be garbage collected
if they have a Qt::Widget parent in the Qt::Object heirarchy. So this is
fine:
self.layout = Qt::VBoxLayout.new do |v|
@button = Qt::PushButton.new "Hello World!"
@edit = Qt::LineEdit.new "Hello?"
v.addWidget @button
v.addWidget @edit
end
On Wednesday 05 April 2006 20:58, Caleb Tennis wrote:
> I think making sure you specify the parent is probably more Qtish:
>
> @button = Qt::PushButton.new("Hello World!", self)
Once the layout is added to the widget, the layout and its widgets are
children of MyWidget, so you don't really need to do this:
p @button.parent
#<MyDialog:0x3009fcb8 objectName="", x=256, y=230, width=512, height=307>
p self.layout
#<Qt::VBoxLayout:0x3009f6d0 objectName="">
p self.layout.parent
#<MyDialog:0x3009fcb8 objectName="", x=256, y=230, width=512, height=307>
-- Richard
More information about the Kde-bindings
mailing list