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

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

reference documentation added

File size: 10.0 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:670 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Qt Tutorial - Chapter 7: One Thing Leads to Another</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 7: One Thing Leads to Another</h1>
33
34
35<p> <center><img src="t7.png" alt="Screenshot of tutorial seven"></center>
36<p> This example shows how to create custom widgets with signals and
37slots, and how to connect them together in more complex ways. For the
38first time, the source is split among several files which we've placed
39in the <tt>t7</tt> subdirectory.
40<p> <ul>
41<li> <a href="t7-lcdrange-h.html">t7/lcdrange.h</a> contains the LCDRange class definition.
42<li> <a href="t7-lcdrange-cpp.html">t7/lcdrange.cpp</a> contains the LCDRange implementation.
43<li> <a href="t7-main-cpp.html">t7/main.cpp</a> contains MyWidget and main.
44</ul>
45<p> <h2> Line-by-line Walkthrough
46</h2>
47<a name="1"></a><p> <h3> <a href="t7-lcdrange-h.html">t7/lcdrange.h</a>
48</h3>
49<a name="1-1"></a><p> This file is mainly lifted from <a href="tutorial1-06.html#main">main.cpp</a> in
50Chapter 6; only the changes are noted here.
51<p>
52
53<p> <pre> #ifndef LCDRANGE_H
54 #define LCDRANGE_H
55</pre>
56<p> This is the classic C construction to avoid errors if a header file
57happens to be included more than once. If you don't use it already,
58it is a very good habit to develop. The #ifndef should enclose <em>all</em> of the
59header file.
60<p> <pre> #include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
61</pre>
62<p> <a href="qvbox-h.html">qvbox.h</a> is included. LCDRange inherits <a href="qvbox.html">QVBox</a>, and the header file
63of a parent class must always be included. We cheated a bit in the
64previous chapters, and we let <a href="qwidget-h.html">qwidget.h</a> be included indirectly via
65other header files such as <a href="qpushbutton-h.html">qpushbutton.h</a>.
66<p> <pre> class QSlider;
67</pre>
68<p> This is another classic trick, but one that's much less used often. Because
69we don't need <a href="qslider.html">QSlider</a> in the <em>interface</em> of the class, only in the
70implementation, we use a forward declaration of the class in the
71header file and include the header file for QSlider in the .cpp
72file.
73<p> This makes the compilation of big projects much faster, because when a
74header file has changed, fewer files need to be recompiled. It can
75often speed up big compilations by a factor of two or more.
76<p> <pre> class LCDRange : public <a href="qvbox.html">QVBox</a>
77 {
78 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
79 public:
80 LCDRange( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
81</pre>
82<p> Note the Q_OBJECT. This macro must be included in <em>all</em> classes that
83contain signals and/or slots. If you are curious, it defines the
84functions that are implemented in the
85<a href="metaobjects.html">meta object file</a>.
86<p> <pre> int value() const;
87 public slots:
88 void setValue( int );
89
90 signals:
91 void valueChanged( int );
92</pre>
93<p> These three members make up an interface between this widget and other
94components in a program. Until now, LCDRange didn't really have an
95interface at all.
96<p> value() is a public function for accessing the value of the LCDRange.
97setValue() is our first custom slot and valueChanged() is our first
98custom signal.
99<p> Slots must be implemented in the normal way (remember that a slot is also
100a C++ member function). Signals are automatically implemented in the
101<a href="signalsandslots.html">meta object</a> file. Signals follow the
102access rules of protected C++ functions (i.e., they can be emitted only
103by the class they are defined in or by classes inheriting from it).
104<p> The signal valueChanged() is used when the LCDRange's value has
105changed - just as you guessed from the name. This is not the last
106signal you'll see called <i>something</i>Changed().
107<p> <h3> <a href="t7-lcdrange-cpp.html">t7/lcdrange.cpp</a>
108</h3>
109<a name="1-2"></a><p>
110
111<p> This file is mainly lifted from <a href="tutorial1-06.html#main">t6/main.cpp</a>, and
112only the changes are noted here.
113<p> <pre> <a name="x2333"></a> <a href="qobject.html#connect">connect</a>( slider, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)),
114 <a name="x2330"></a> lcd, SLOT(<a href="qlcdnumber.html#display">display</a>(int)) );
115 <a href="qobject.html#connect">connect</a>( slider, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)),
116 SIGNAL(valueChanged(int)) );
117</pre>
118<p> This code is from the LCDRange constructor.
119<p> The first connect is the same that you have seen in the previous chapter.
120The second is new; it connects slider's valueChanged() signal to this
121object's valueChanged <em>signal</em>. Connect() with 3 arguments always
122connects to signals or slots in <tt>this</tt> object.
123<p> Yes, that's right. Signals can be connected to other signals. When
124the first is emitted, the second signal is also emitted.
125<p> Let's look at what happens when the user operates the slider. The
126slider sees that its value has changed and emits the valueChanged()
127signal. That signal is connected both to the display() slot of the
128<a href="qlcdnumber.html">QLCDNumber</a> and to the valueChanged() signal of the LCDRange.
129<p> Thus, when the signal is emitted, LCDRange emits its own
130valueChanged() signal. In addition, <a href="qlcdnumber.html#display">QLCDNumber::display</a>() is called
131and shows the new number.
132<p> Note that you're not guaranteed any particular order of execution -
133LCDRange::valueChanged() may be emitted before or after
134QLCDNumber::display()and is entirely arbitrary.
135<p> <pre> int LCDRange::value() const
136 {
137 <a name="x2332"></a> return slider-&gt;<a href="qslider.html#value">value</a>();
138 }
139</pre>
140<p> The implementation of value() is straightforward; it simply returns
141the slider's value.
142<p> <pre> void LCDRange::setValue( int value )
143 {
144 <a name="x2331"></a> slider-&gt;<a href="qslider.html#setValue">setValue</a>( value );
145 }
146</pre>
147<p> The implementation of setValue() is equally straightforward. Note
148that because the slider and LCD number are connected, setting the
149slider's value automatically updates the LCD number as well. In
150addition, the slider will automatically adjust the value if it is
151outside its legal range.
152<p> <h3> <a href="t7-main-cpp.html">t7/main.cpp</a>
153</h3>
154<a name="1-3"></a><p>
155
156<p> <pre> LCDRange *previous = 0;
157 for( int r = 0 ; r &lt; 4 ; r++ ) {
158 for( int c = 0 ; c &lt; 4 ; c++ ) {
159 LCDRange* lr = new LCDRange( grid );
160 if ( previous )
161 <a href="qobject.html#connect">connect</a>( lr, SIGNAL(valueChanged(int)),
162 previous, SLOT(setValue(int)) );
163 previous = lr;
164 }
165 }
166</pre>
167<p> All of main.cpp is copied from the previous chapter except in
168the constructor for MyWidget. When we create the 16 LCDRange object, we
169now connect them using the <a href="signalsandslots.html">signal/slot</a> mechanism. Each has its valueChanged() signal
170connected to the setValue() slot in the previous one. Because LCDRange
171emits the signal valueChanged() when its value changes (surprise!), we
172are here creating a "chain" of signals and slots.
173<p> <a name="compiling"></a>
174<h2> Compiling
175</h2>
176<a name="2"></a><p> Creating a makefile for a multi-file application is no different from
177creating one for a single-file application. If you've saved all the
178files in this example in their own directory, all you have to do is:
179<pre>
180qmake -project
181qmake
182</pre>
183
184<p> The first command tells <a href="qmake-manual.html">qmake</a> to
185create a <tt>.pro</tt> (project) file. The second command tells it to create
186a (platform-specific) makefile based on the project file. You should
187now be able to type <tt>make</tt> (or <tt>nmake</tt> if you're using Visual
188Studio) to build your application.
189<p> <h2> Behavior
190</h2>
191<a name="3"></a><p> On startup, the program's appearance is identical to the previous one.
192Try operating the slider to the bottom right...
193<p> <h2> Exercises
194</h2>
195<a name="4"></a><p> Use the bottom right slider to set all LCDs to 50. Then set the top
196half to 40 by clicking once to the left of the slider handle. Now,
197use the one to the left of the last one operated to set the first
198seven LCDs back to 50.
199<p> Click to the left of the handle on the bottom right slider. What
200happens? Why is this the correct behavior?
201<p> You're now ready for <a href="tutorial1-08.html">Chapter 8.</a>
202<p> [<a href="tutorial1-06.html">Previous tutorial</a>]
203[<a href="tutorial1-08.html">Next tutorial</a>]
204[<a href="tutorial.html">Main tutorial page</a>]
205<p>
206<!-- eof -->
207<p><address><hr><div align=center>
208<table width=100% cellspacing=0 border=0><tr>
209<td>Copyright &copy; 2007
210<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
211<td align=right><div align=right>Qt 3.3.8</div>
212</table></div></address></body>
213</html>
Note: See TracBrowser for help on using the repository browser.