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  ·  Tous les espaces de nom  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Qt Tutorial 1 - Hello World!

Files:

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 below is a screenshot of this program.

Screenshot of Chapter 1

Here's the complete source code for the application:

 #include <QApplication>
 #include <QPushButton>

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);

     QPushButton hello("Hello world!");

     hello.show();
     return app.exec();
 }

Line by Line Walkthrough

 #include <QApplication>

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

 #include <QPushButton>

This line includes the QPushButton class definition. For each class that's part of the public Qt API, there exists a header file of the same name that contains its definition.

QPushButton is a GUI push button that the user can press and release. It manages its own look and feel, like every other QWidget. A widget is a user interface object that 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 QIcon.

 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.

The argc parameter is the number of command-line arguments and argv is the array of command-line arguments. This is a standard C++ feature.

     QApplication app(argc, argv);

The app object is this program's QApplication instance. Here it is created. We pass argc and argv to the QApplication constructor so that it can process certain standard command-line arguments (such as -display under X11). All command-line arguments recognized by Qt are removed from argv, and argc is decremented accordingly. See the QApplication::arguments() documentation for details.

The QApplication object must be created before any GUI-related features of Qt are used.

     QPushButton hello("Hello world!");

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

The button is set up to display the text "Hello world!". Because we don't specify a parent window (as second argument to the QPushButton constructor), the button will be a window of its own, with its own window frame and title bar.

The button's size is determined by its default size hint. We could call QWidget::move() to assign a specific screen position to the widget, but instead we let the windowing system choose a position.

     hello.show();

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

     return app.exec();
 }

This is where main() passes control to Qt. QCoreApplication::exec() will return when the application exits. (QCoreApplication is QApplication's base class. It implements QApplication's core, non-GUI functionality and can be used when developing non-GUI applications.)

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

You should now compile and run this program.

Environment Variables

If you are developing Qt applications at the command line, you need to ensure that the Qt libraries and executables are accessible to your environment by prepending the path to Qt's bin directory to your PATH variable; this is described in the Installation instructions for your platform.

On Windows, this is automatically done for you if you get your command line prompt from the Start>All Programs>Qt menu. If you get your command line prompt with Start>Run>Command, you have to set the PATH variable there yourself.

Building an application

The tutorial examples are located in the Qt examples/tutorials/tutorial directory. If you installed a binary Qt package, pre-built examples were installed as well. If you built Qt yourself, the examples were built at the same time. In either case, there is a lot to be learned about using Qt by modifying and building the examples yourself.

Assuming you have copied an example's .cpp and .h files to an otherwise empty directory, and you have made your changes there, the next step is to create a Qt makefile in that directory. To create a Qt makefile, use qmake, the build tool supplied with Qt. Run the following two commands in the directory containing your modified sources to create the makefile.

 qmake -project
 qmake

The first command tells qmake to create a project (.pro) file. The second command tells qmake to use the .pro file to create a platform-specific makefile.

Now you can just run make (nmake if you are using Visual Studio) to compile the program, and then you can run your first Qt application!

Running this example

When you run the example, you will see a small window showing a single button. On the button you can read the famous words: "Hello world!"

Exercises

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

[Previous: Qt Tutorial] [Qt Tutorial] [Next: Chapter 2]

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 53
  2. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  3. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Labs au hasard

Logo

Le coût des commodités

Les Qt Labs sont les laboratoires des développeurs de Qt, où ils peuvent partager des impressions sur le framework, son utilisation, ce que pourrait être son futur. 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 4.4
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