Qt Tutorial - Chapter 2: Calling it Quits
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.
/**************************************************************** ** ** Qt tutorial 2 ** ****************************************************************/ #include <qapplication.h> #include <qpushbutton.h> #include <qfont.h> int main( int argc, char **argv ) { QApplication a( argc, argv ); QPushButton quit( "Quit", 0 ); quit.resize( 75, 30 ); quit.setFont( QFont( "Times", 18, QFont::Bold ) ); QObject::connect( &quit, SIGNAL(clicked()), &a, SLOT(quit()) ); a.setMainWidget( &quit ); quit.show(); return a.exec(); }
Line-by-line Walkthrough
#include <qfont.h> Since this program uses QFont, it needs to include qfont.h. Qt's font abstraction is rather different from the horror provided by X, and loading and using fonts has been highly optimized.
QPushButton quit( "Quit", 0 ); This time, the button says "Quit" and that's exactly what the program will do when the user clicks the button. This is not a coincidence. We still pass 0 as the parent, since the button is a top-level window.
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.
Here we choose a new font for the button, an 18-point bold font from the Times family. Note that we create the font on the spot. It is also possible to change the default font (using QApplication::setFont()) for the whole application.
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 socket library. This line 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. They inherit QWidget which in turn inherits QObject. Here, the clicked() signal of quit is connected to the quit() slot of a, so that when the button is clicked, the application quits. The Signals and Slots documentation describes this topic in detail.
BehaviorWhen you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button. (See Compiling for how to create a makefile and build the application.)
ExercisesTry to resize the window. Press the button. Oops! That connect() would seem to make some difference. Are there any other signals in QPushButton you can connect to quit? Hint: The QPushButton inherits most of its behavior from QButton. You're now ready for Chapter 3. [Previous tutorial] [Next tutorial] [Main tutorial page]
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le blog Digia au hasardCréer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut CuvelierLe blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.
CommunautéRessources
Liens utilesContact
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 3.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 ! |
Copyright © 2000-2012 - www.developpez.com