Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Classes  ·  Annotées  ·  Hiérarchie  ·  Fonctions  ·  Structure  · 

Chapter 1: Hello, World!


Screenshot of tutorial one

This first program is a simple hello-world example. It contains only the bare minimum you need to get a Qt application up and running. The picture above is a snapshot of this program.

/****************************************************************
**
** Qt tutorial 1
**
****************************************************************/

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

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

    QPushButton hello( "Hello world!", 0 );
    hello.resize( 100, 30 );

    a.setMainWidget( &hello );
    hello.show();
    return a.exec();
}

Line by Line Walk-Through

    #include <qapplication.h>

This line includes the QApplication class definition. There has to be exactly one QApplication object in every application that uses Qt. QApplication manages various application-wide resources, such as the default font and cursor.

    #include <qpushbutton.h>

This line includes the QPushButton class definition. The reference documentation for each class mentions at the top which file needs to be included to use that class.

QPushButton is a classical GUI push button, which the user can press and release. It manages its own look and feel, like every other QWidget. A widget is a user interface object which can process user input and draw graphics. The programmer can change both the overall look and feel and many minor properties of it such as color, as well as the widget's content. A QPushButton can show either a text or a QPixmap.

    int main( int argc, char **argv )
    {

The main() function is the entry point to the program. Almost always when using Qt, main() only needs to perform some kind of initialization before passing the control to the Qt library, which then tells the program about the user's actions via events.

argc is the number of command-line arguments and argv is the array of command-line arguments. This is a C/C++ feature. It is not Qt specific, however, Qt needs to process these arguments (see below).

        QApplication a( argc, argv );

a is this program's QApplication. Here it is created and processes some of the command-line arguments (such as -display under X11). Note that all command-line arguments recognized by Qt are removed from argv (and argc is decremented accordingly). See the QApplication::argv() documentation for details.

Note: It is essential that the QApplication object is created before any window-system parts of Qt are used.

        QPushButton hello( "Hello world!", 0 );

Here, after the QApplication, comes the first window-system code: A push button is created.

The button is set up to display the text "Hello world!" and be a window of its own (since the constructor specifies 0 for the parent window which the button should be inside).

        hello.resize( 100, 30 );

The button is set up to be 100 pixels wide and 30 pixels high (plus the window system frame). In this case we don't care about the button's position and accept the default value.

        a.setMainWidget( &hello );

The push button is chosen as the main widget for the application. If the user closes a main widget, the application exits.

You don't have to have a main widget, but most programs have one.

        hello.show();

A widget is never visible when you create it. You must call show() to make it visible.

        return a.exec();

This is where main() passes control to Qt. exec() will return when the application exits.

In exec(), Qt receives and processes user and system events and passes these on to the appropriate widgets.

    }

Behavior

You should now try to compile and run this program.

When you run it, you will see a small window filled with a single button, and on it you can read the famous words, Hello World!

Exercises

Try to resize the window. Press the button. If you're running X11, try running the program with the -geometry option (e.g. -geometry 100x200+10+20)

You may now go on to chapter two.

[Next tutorial] [Main tutorial page]

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 94
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 47
  4. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  5. Les développeurs détestent-ils les antivirus ? Un programmeur manifeste sa haine envers ces solutions de sécurité 13
  6. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  7. Qt Commercial : Digia organise un webinar gratuit le 27 mars sur la conception d'interfaces utilisateur et d'applications avec le framework 0
Page suivante

Le Qt Quarterly au hasard

Logo

Le tri des QListView

Qt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 2.3
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web