KDE Tutorial - p1

p1

Next Previous Table of Contents

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


#include <qapplication.h>
#include <qpushbutton.h>

int main( int argc, char **argv )
{
    QApplication a( argc, argv );

    QPushButton *hello=new QPushButton( "Hello world!", 0 );
    hello->resize( 100, 30 );
 
    QObject::connect( hello, SIGNAL(clicked()), &a, SLOT(quit()) );
 
    a.setMainWidget( hello );
    hello->show();
 
    return a.exec();
} 

main.cpp

Let's see what does each line do.

    QApplication a( argc, argv );

This code creates a QApplication 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.

    QPushButton *hello=new QPushButton( "Hello world!", 0 );
    hello->resize( 100, 30 );

With this code, we create a QPushButton object. This is a button which you can click (instead of a button which has an state, as the QRadioButtons or a QCheckBox).

It contains the text "Hello world!", and it has no parent ( 0 is being passed as a NULL pointer ). 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.

    QObject::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 inherit from QObject (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 QPushButton, it emits the clicked() signal or when a QScrollBar is scrolled, it emits a valueChanged() signal . On the other hand, a slot is a normal member 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 evenmore, 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.

The code that is needed to call the slots is generated by moc along with other code that initializes the meta object information . We'll see later how to use moc, but as we only use existing classes in p1, we don't have to run moc yet.

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.

    return 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,2000,2001 Antonio Larrosa