Qt Tutorial - Chapter 5: Building Blocks
This example shows how to create and connect together several widgets by using signals and slots, and how to handle resize events.
/**************************************************************** ** ** Qt tutorial 5 ** ****************************************************************/ #include <qapplication.h> #include <qpushbutton.h> #include <qslider.h> #include <qlcdnumber.h> #include <qfont.h> #include <qvbox.h> class MyWidget : public QVBox { public: MyWidget( QWidget *parent=0, const char *name=0 ); };
Line-by-line Walkthrough
#include <qapplication.h> #include <qpushbutton.h> #include <qslider.h> #include <qlcdnumber.h> #include <qfont.h> #include <qvbox.h> Three new include files are shown here. qslider.h and qlcdnumber.h are there because we use two new widgets, QSlider and QLCDNumber. qvbox.h is here because we use Qt's automatic layout support.
class MyWidget : public QVBox { public: MyWidget( QWidget *parent=0, const char *name=0 ); }; MyWidget is now derived from QVBox instead of QWidget. That way we use
the layout of the QVBox (which places all of its children vertically
inside itself). Resizes are now handled automatically by the QVBox and
therefore by MyWidget, too.
lcd is a QLCDNumber, a widget that displays numbers in an LCD-like
fashion. This instance is set up to display two digits and to be a child of
this. It is named "lcd".
QSlider is a classical slider; the user can use the widget to drag
something to adjust an integer value in a range. Here we create a
horizontal one, set its range to 0-99 (inclusive, see the QSlider::setRange() documentation) and its initial value to 0.
Here we use the signal/slot mechanism
to connect the slider's valueChanged() signal to the LCD number's
display() slot.
Whenever the slider's value changes it broadcasts the new value by
emitting the valueChanged() signal. Because that signal is connected to
the LCD number's display() slot, the slot is called when the signal is
broadcast. Neither of the objects knows about the other. This is
essential in component programming.
Slots are otherwise normal C++ member functions and follow the normal
C++ access rules.
The LCD number reflects everything you do to the slider, and the
widget handles resizing well. Notice that the LCD number widget
changes in size when the window is resized (because it can), but the
others stay about the same (because otherwise they would look stupid).
(See Compiling for how to create a
makefile and build the application.)
Try changing the LCD number to add more digits or to change mode. You can even add four push
buttons to set the number base.
You can also change the slider's range.
Perhaps it would have been better to use QSpinBox than a slider?
Try to make the application quit when the LCD number overflows.
You're now ready for Chapter 6.
[Previous tutorial]
[Next tutorial]
[Main tutorial page]
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le Qt Quarterly au hasardRequête de données génériques avec QtXmlPatterns et XQueryQt 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 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