source: trunk/doc/html/tutorial1-05.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 8.1 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial.doc:487 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Qt Tutorial - Chapter 5: Building Blocks</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Qt Tutorial - Chapter 5: Building Blocks</h1>
33
34
35<p> <center><img src="t5.png" alt="Screenshot of tutorial five"></center>
36<p> This example shows how to create and connect together several widgets
37by using signals and slots, and how to handle resize events.
38<p> <pre>/****************************************************************
39**
40** Qt tutorial 5
41**
42****************************************************************/
43
44#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
45#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
46#include &lt;<a href="qslider-h.html">qslider.h</a>&gt;
47#include &lt;<a href="qlcdnumber-h.html">qlcdnumber.h</a>&gt;
48#include &lt;<a href="qfont-h.html">qfont.h</a>&gt;
49
50#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
51
52class MyWidget : public <a href="qvbox.html">QVBox</a>
53{
54public:
55 MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
56};
57
58
59<a name="f553"></a>MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
60 : <a href="qvbox.html">QVBox</a>( parent, name )
61{
62 <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );
63 quit-&gt;<a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
64
65 <a href="qobject.html#connect">connect</a>( quit, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
66
67 <a href="qlcdnumber.html">QLCDNumber</a> *lcd = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "lcd" );
68
69 <a href="qslider.html">QSlider</a> * slider = new <a href="qslider.html">QSlider</a>( Horizontal, this, "slider" );
70 slider-&gt;<a href="qrangecontrol.html#setRange">setRange</a>( 0, 99 );
71 slider-&gt;<a href="qslider.html#setValue">setValue</a>( 0 );
72
73 <a href="qobject.html#connect">connect</a>( slider, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)), lcd, SLOT(<a href="qlcdnumber.html#display">display</a>(int)) );
74}
75
76int main( int argc, char **argv )
77{
78 <a href="qapplication.html">QApplication</a> a( argc, argv );
79
80 MyWidget w;
81 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;w );
82 w.<a href="qwidget.html#show">show</a>();
83 return a.<a href="qapplication.html#exec">exec</a>();
84}
85</pre>
86
87
88
89<p> <h2> Line-by-line Walkthrough
90</h2>
91<a name="1"></a><p> <pre> #include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
92 #include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
93 #include &lt;<a href="qslider-h.html">qslider.h</a>&gt;
94 #include &lt;<a href="qlcdnumber-h.html">qlcdnumber.h</a>&gt;
95 #include &lt;<a href="qfont-h.html">qfont.h</a>&gt;
96
97 #include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
98</pre>
99<p> Three new include files are shown here. qslider.h and qlcdnumber.h are there
100because we use two new widgets, <a href="qslider.html">QSlider</a> and <a href="qlcdnumber.html">QLCDNumber</a>. qvbox.h is
101here because we use Qt's automatic layout support.
102<p> <pre> class MyWidget : public <a href="qvbox.html">QVBox</a>
103 {
104 public:
105 MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
106 };
107</pre>
108<p> <a name="constructor"></a>
109<pre> MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
110 : <a href="qvbox.html">QVBox</a>( parent, name )
111 {
112</pre>
113<p> MyWidget is now derived from <a href="qvbox.html">QVBox</a> instead of <a href="qwidget.html">QWidget</a>. That way we use
114the layout of the QVBox (which places all of its children vertically
115inside itself). Resizes are now handled automatically by the QVBox and
116therefore by MyWidget, too.
117<p> <pre> <a href="qlcdnumber.html">QLCDNumber</a> *lcd = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "lcd" );
118</pre>
119<p> <tt>lcd</tt> is a QLCDNumber, a widget that displays numbers in an LCD-like
120fashion. This instance is set up to display two digits and to be a child of
121<em>this</em>. It is named "lcd".
122<p> <pre> <a href="qslider.html">QSlider</a> * slider = new <a href="qslider.html">QSlider</a>( Horizontal, this, "slider" );
123 <a name="x2315"></a> slider-&gt;<a href="qrangecontrol.html#setRange">setRange</a>( 0, 99 );
124 <a name="x2316"></a> slider-&gt;<a href="qslider.html#setValue">setValue</a>( 0 );
125</pre>
126<p> <a href="qslider.html">QSlider</a> is a classical slider; the user can use the widget to drag
127something to adjust an integer value in a range. Here we create a
128horizontal one, set its range to 0-99 (inclusive, see the <a href="qrangecontrol.html#setRange">QSlider::setRange</a>() documentation) and its initial value to 0.
129<p> <pre> <a name="x2317"></a><a name="x2314"></a> <a href="qobject.html#connect">connect</a>( slider, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)), lcd, SLOT(<a href="qlcdnumber.html#display">display</a>(int)) );
130</pre>
131<p> Here we use the <a href="signalsandslots.html">signal/slot mechanism</a>
132to connect the slider's valueChanged() signal to the LCD number's
133display() slot.
134<p> Whenever the slider's value changes it broadcasts the new value by
135emitting the valueChanged() signal. Because that signal is connected to
136the LCD number's display() slot, the slot is called when the signal is
137broadcast. Neither of the objects knows about the other. This is
138essential in component programming.
139<p> Slots are otherwise normal C++ member functions and follow the normal
140C++ access rules.
141<p> <h2> Behavior
142</h2>
143<a name="2"></a><p> The LCD number reflects everything you do to the slider, and the
144widget handles resizing well. Notice that the LCD number widget
145changes in size when the window is resized (because it can), but the
146others stay about the same (because otherwise they would look stupid).
147<p> (See <a href="tutorial1-01.html#compiling">Compiling</a> for how to create a
148makefile and build the application.)
149<p> <h2> Exercises
150</h2>
151<a name="3"></a><p> Try changing the LCD number to add more digits or <a href="qlcdnumber.html#setMode">to change mode.</a> You can even add four push
152buttons to set the number base.
153<p> You can also change the slider's range.
154<p> Perhaps it would have been better to use <a href="qspinbox.html">QSpinBox</a> than a slider?
155<p> Try to make the application quit when the LCD number overflows.
156<p> You're now ready for <a href="tutorial1-06.html">Chapter 6.</a>
157<p> [<a href="tutorial1-04.html">Previous tutorial</a>]
158[<a href="tutorial1-06.html">Next tutorial</a>]
159[<a href="tutorial.html">Main tutorial page</a>]
160<p>
161<!-- eof -->
162<p><address><hr><div align=center>
163<table width=100% cellspacing=0 border=0><tr>
164<td>Copyright &copy; 2007
165<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
166<td align=right><div align=right>Qt 3.3.8</div>
167</table></div></address></body>
168</html>
Note: See TracBrowser for help on using the repository browser.