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 for graphical user interface (GUI) applications built with Qt. Each GUI component (e.g. buttons, labels, text editor) is a widget that is placed somewhere within a user interface window, or is displayed as an independent window. Each type of widge is provided by a 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 it can be subclassed with minimal effort to create new, custom widgets. QWidget is often used to create a window inside which other QWidgets are placed.

As with QObjects, QWidgets 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 widget is displayed within the screen area occupied by its parent widget. This means that when you delete a window widget, all the child widgets it contains are also deleted.

Writing a main Function

Many of the GUI examples provided with Qt follow the pattern of having a main.cpp file, which contains the standard code to initialize the application, plus any number of other source/header files that contain the application logic and custom GUI components.

A typical main() function 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();
 }

First, a QApplication object is constructed, which can be configured with arguments passed in from the command line. After the widgets have been created and shown, QApplication::exec() is called to start Qt's event loop. Control passes to Qt until this function returns. Finally, main() returns the value returned by QApplication::exec().

Simple widget examples

Each of theses simple widget examples is written entirely within the main() function.

Real world widget examples

In these more advanced examples, the code that creates the widgets and layouts is stored in other files. For example, the GUI for a main window may be created in the constructor of a QMainWindow subclass.

Building The Examples

If you installed a binary package to get Qt, or if you compiled Qt yourself, the examples described in this tutorial should already be built and ready to run. If you wish to modify and recompile them, follow these steps:

  1. From a command prompt, enter the directory containing the example you have modified.
  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 is created in the current directory. On Windows, this file may be located in a debug or release subdirectory. You can run this executable to see the example code at work.

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.8
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