Ruby Logo Home

p1

Next Previous Table of Contents

Screenshot of tutorial one

p1 is the one of the simplest Qt applications, all the code is in p1.rb:


require 'Qt'

    a = Qt::Application.new( ARGV )

    hello = Qt::PushButton.new( "Hello world!", nil )
    hello.resize( 100, 30 )
 
    Qt::Object::connect( hello, SIGNAL('clicked()'), a, SLOT('quit()') )
 
    a.setMainWidget( hello )
    hello.show()
 
    a.exec()


p1.rb

Let's see what does each line does.

    a = Qt::Application.new( ARGV )

This code creates a Qt::Application object that will control the execution flow of the application, most of the general settings of the system and the application (for a more uniform desktop), etc.

    hello = Qt::PushButton.new( "Hello world!", nil )
    hello.resize( 100, 30 )

With this code, we create a Qt::PushButton object. This is a button which you can click (instead of a button which has a state, such as a Qt::RadioButtons or a Qt::CheckBox).

It contains the text "Hello world!", and it has no parent (nil is being passed). By using a widget with no parent, we are letting the window manager manage this widget as another window in the desktop. Usually, applications only have a single window with no parent, which is the main widget, but we will see more about this later.

We also resize the widget to have 100 pixels wide and 30 pixels high.

    Qt::Object::connect( hello, SIGNAL('clicked()'), a, SLOT('quit()') )

To explain this code we should have a little background about the signal/slot mechanism. I'll try to explain it here, but if you don't understand this explanation, you should read the Qt excellent "Introduction to Signals and Slots".

Every class that inherits from Qt::Object (directly or indirectly by inheriting a subclass of it) can contain signals and/or slots. An object emits a signal whenever it changes its state in any way. For example, when you click on a Qt::PushButton, it emits the clicked() signal or when a Qt::ScrollBar is scrolled, it emits a valueChanged() signal. On the other hand, a slot is a normal instance method in an object that can receive signals.

An object doesn't have to know if a slot is connected to one of its signals and furthermore, you can connect a single signal to multiple slots or multiple signals to a single slot, this allows for real reusable components in the development process. Finally, the code above is used to connect the "clicked()" signal from the object hello to the "quit()" slot of the application (a) which closes the application.

    a.setMainWidget( hello )
    hello.show()

This code makes hello the main widget of the application (the one which will close the application when destroyed), and shows it on the screen.

    a.exec()

Finally, the event loop is executed and we left the user take control of the application. Whenever the user clicks on the hello button, the event loop will call the application quit() slot and this function will return a return value to the system.

Isn't it easy ? :-)

Next Previous Table of Contents


© 1999-2002 Antonio Larrosa © 2004 Richard Dale