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  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Qt Tutorial 2 - Calling it Quits

Files:

Screenshot of Chapter 2

Having created a window in Chapter 1, we will now go on to make the application quit properly when the user tells it to.

We will also use a font that is more exciting than the default one.

 #include <QApplication>
 #include <QFont>
 #include <QPushButton>

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

     QPushButton quit("Quit");
     quit.resize(75, 30);
     quit.setFont(QFont("Times", 18, QFont::Bold));

     QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));

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

Line by Line Walkthrough

 #include <QFont>

Since this program uses QFont, it needs to include <QFont>.

     QPushButton quit("Quit");

This time, the button says Quit and that's exactly what the program will do when the user clicks the button.

     quit.resize(75, 30);

We've chosen another size for the button since the text is a bit shorter than "Hello world!". We could also have used QFontMetrics to set right size, or let QPushButton choose a reasonable default.

     quit.setFont(QFont("Times", 18, QFont::Bold));

Here we choose a new font for the button, an 18-point bold font from the Times family. It is also possible to change the default font for the entire application, using QApplication::setFont().

     QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));

QObject::connect() is perhaps the most central feature of Qt. Note that connect() is a static function in QObject. Do not confuse it with the connect() function in the Berkeley socket library.

This connect() call establishes a one-way connection between two Qt objects (objects that inherit QObject, directly or indirectly). Every Qt object can have both signals (to send messages) and slots (to receive messages). All widgets are Qt objects, since they inherit QWidget, which in turn inherits QObject.

Here, the clicked() signal of quit is connected to the quit() slot of app, so that when the button is clicked, the application quits.

The Signals and Slots documentation describes this topic in detail.

Running the Application

When you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button.

See Chapter 1 for how to create a makefile and build the application.

Exercises

Try to resize the window. Press the button to close the application.

Are there any other signals in QPushButton you can connect to quit? [Hint: The QPushButton inherits most of its functionality from QAbstractButton.]

[Previous: Chapter 1] [Qt Tutorial] [Next: Chapter 3]

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 64
  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. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  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 Developer Network au hasard

Logo

Comment fermer une application

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. 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.2
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