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  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

Widgets Tutorial

Introduction

Widgets are the basic building blocks of graphical user interface (GUI) applications made with Qt. Each GUI component, such as a button, label or text editor, is a widget and can be placed within an existing user interface or displayed as an independent window. Each type of component is provided by a particular subclass of QWidget, which is itself a subclass of QObject.

QWidget is not an abstract class; it can be used as a container for other widgets, and can be subclassed with minimal effort to create custom widgets. It is most often used to create windows in which other widgets are placed.

As with QObjects, widgets can be created with parent objects to indicate ownership, ensuring that objects are deleted when they are no longer used. With widgets, these parent-child relationships have an additional meaning: each child is displayed within the screen area occupied by its parent. This means that, when a window is deleted, all the widgets it contains are automatically deleted.

Writing a main Function

Many of the GUI examples in Qt follow the pattern of having a main.cpp file containing code to initialize the application, and a number of other source and header files containing the application logic and custom GUI components.

A typical main() function, written in main.cpp, looks like this:

 #include <QtGui>

 // Include header files for application components.
 // ...

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

     // Set up and show widgets.
     // ...

     return app.exec();
 }

We first construct a QApplication object which is configured using any arguments passed in from the command line. After any widgets have been created and shown, we call QApplication::exec() to start Qt's event loop. Control passes to Qt until this function returns, at which point we return the value we obtain from this function.

In each part of this tutorial, we provide an example that is written entirely within a main() function. In more sophisticated examples, the code to set up widgets and layouts is written in other parts of the example. For example, the GUI for a main window may be set up in the constructor of a QMainWindow subclass.

The Widgets examples are a good place to look for more complex and complete examples and applications.

Building Examples and Tutorials

If you obtained a binary package of Qt or compiled it yourself, the examples described in this tutorial should already be ready to run. However, if you may wish to modify them and recompile them, you need to perform the following steps:

  1. At the command line, enter the directory containing the example you wish to recompile.
  2. Type qmake and press Return. If this doesn't work, make sure that the executable is on your path, or enter its full location.
  3. On Linux/Unix and Mac OS X, type make and press Return; on Windows with Visual Studio, type nmake and press Return.

An executable file should have been created within the current directory. On Windows, this file may be located within a debug or release subdirectory. You can run this file to see the example code at work.

[Tutorials] [Next: Creating a Window]

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 44
  2. Microsoft ouvre aux autres compilateurs C++ AMP, la sp�cification pour la conception d'applications parall�les C++ utilisant le GPU 22
  3. Les d�veloppeurs ignorent-ils trop les failles d�couvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. RIM : � 13 % des d�veloppeurs ont gagn� plus de 100 000 $ sur l'AppWord �, Qt et open-source au menu du BlackBerry DevCon Europe 0
  5. 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
  6. Quelles nouveaut�s de C++11 Visual C++ doit-il rapidement int�grer ? Donnez-nous votre avis 10
  7. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil d�claratif et extensible pour la compilation de projets Qt 17
Page suivante

Le Qt Quarterly au hasard

Logo

XQuery et la m�t�o

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 4.6
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