~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/tutorial-t7.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/tutorial.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Qt Tutorial 7 - One Thing Leads to Another</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
    <link rel="prev" href="tutorial-t6.html" />
 
15
    <link rel="contents" href="tutorial.html" />
 
16
    <link rel="next" href="tutorial-t8.html" />
 
17
</head>
 
18
<body>
 
19
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
20
<tr>
 
21
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
22
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
23
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><p>
 
24
[Previous: <a href="tutorial-t6.html">Chapter 6</a>]
 
25
[<a href="tutorial.html">Qt Tutorial</a>]
 
26
[Next: <a href="tutorial-t8.html">Chapter 8</a>]
 
27
</p>
 
28
<h1 align="center">Qt Tutorial 7 - One Thing Leads to Another</h1>
 
29
<p>Files:</p>
 
30
<ul>
 
31
<li><a href="tutorial-t7-lcdrange-cpp.html">tutorial/t7/lcdrange.cpp</a></li>
 
32
<li><a href="tutorial-t7-lcdrange-h.html">tutorial/t7/lcdrange.h</a></li>
 
33
<li><a href="tutorial-t7-main-cpp.html">tutorial/t7/main.cpp</a></li>
 
34
</ul>
 
35
<center><img src="images/t7.png" alt="Screenshot of tutorial seven" /></center><p>This example shows how to create custom widgets with signals and slots, and how to connect them together in more complex ways. For the first time, the source is split among several files which we've placed in the <tt>examples/tutorial/t7</tt> directory.</p>
 
36
<a name="line-by-line-walkthrough"></a>
 
37
<h2>Line by Line Walkthrough</h2>
 
38
<a name="t7-lcdrange-h"></a>
 
39
<h3><a href="tutorial-t7-lcdrange-h.html">t7/lcdrange.h</a></h3>
 
40
<p>This file is mainly lifted from <tt>main.cpp</tt> in Chapter 6; only the non-trivial changes are noted here.</p>
 
41
<pre>&nbsp;   #ifndef LCDRANGE_H
 
42
    #define LCDRANGE_H</pre>
 
43
<p>This, together with the <tt>#endif</tt> at the end of the file, is the standard C++ construction to avoid errors if a header file happens to be included more than once. If you don't use it already, it is a very good habit to develop.</p>
 
44
<pre>&nbsp;   #include &lt;QWidget&gt;</pre>
 
45
<p><tt>&lt;QWidget&gt;</tt> is included since our <tt>LCDRange</tt> class inherits <a href="qwidget.html">QWidget</a>. The header file of a parent class must always be included - we cheated a bit in the previous chapters, and we let <tt>&lt;QWidget&gt;</tt> be included indirectly via other header files.</p>
 
46
<pre>&nbsp;   class QSlider;</pre>
 
47
<p>This is another classic trick, but one that's much less used often. Because we don't need <a href="qslider.html">QSlider</a> in the interface of the class, only in the implementation, we use a forward declaration of the class in the header file and include the header file for <a href="qslider.html">QSlider</a> in the <tt>.cpp</tt> file.</p>
 
48
<p>This makes the compilation of big projects much faster, because the compiler usually spends most of its time parsing header files, not the actual source code. This trick alone can often speed up compilations by a factor of two or more.</p>
 
49
<pre>&nbsp;   class LCDRange : public QWidget
 
50
    {
 
51
        Q_OBJECT
 
52
 
 
53
    public:
 
54
        LCDRange(QWidget *parent = 0);</pre>
 
55
<p>Note the <tt>Q_OBJECT</tt>. This macro must be included in <i>all</i> classes that contain signals and/or slots. If you are curious, it defines the functions that are implemented in the <a href="metaobjects.html">meta-object file</a>.</p>
 
56
<pre>&nbsp;       int value() const;
 
57
 
 
58
    public slots:
 
59
        void setValue(int value);
 
60
 
 
61
    signals:
 
62
        void valueChanged(int newValue);</pre>
 
63
<p>These three members make up an interface between this widget and other components in a program. Until now, <tt>LCDRange</tt> didn't really have an API at all.</p>
 
64
<p><tt>value()</tt> is a public function for accessing the value of the <tt>LCDRange</tt>, <tt>setValue()</tt> is our first custom slot, and <tt>valueChanged()</tt> is our first custom signal.</p>
 
65
<p>Slots must be implemented in the normal way (a slot is also a C++ member function). Signals are automatically implemented in the meta-object file. Signals follow the access rules of protected C++ functions (i.e., they can be emitted only by the class they are defined in or by classes inheriting from it).</p>
 
66
<p>The <tt>valueChanged()</tt> signal is used when the <tt>LCDRange</tt>'s value has changed.</p>
 
67
<a name="t7-lcdrange-cpp"></a>
 
68
<h3><a href="tutorial-t7-lcdrange-cpp.html">t7/lcdrange.cpp</a></h3>
 
69
<p>This file is mainly lifted from <tt>main.cpp</tt> in Chapter 6, and only the changes are noted here.</p>
 
70
<pre>&nbsp;       connect(slider, SIGNAL(valueChanged(int)),
 
71
                lcd, SLOT(display(int)));
 
72
        connect(slider, SIGNAL(valueChanged(int)),
 
73
                this, SIGNAL(valueChanged(int)));</pre>
 
74
<p>This code is from the <tt>LCDRange</tt> constructor.</p>
 
75
<p>The first <a href="qobject.html#connect">connect()</a> call is the same that you have seen in the previous chapter. The second is new; it connects slider's <a href="qabstractslider.html#valueChanged">valueChanged()</a> signal to this object's <tt>valueChanged()</tt> signal. Yes, that's right. Signals can be connected to other signals. When the first is emitted, the second signal is also emitted.</p>
 
76
<p>Let's look at what happens when the user operates the slider. The slider sees that its value has changed and emits the <a href="qabstractslider.html#valueChanged">valueChanged()</a> signal. That signal is connected both to the <a href="qlcdnumber.html#intValue-prop">display()</a> slot of the <a href="qlcdnumber.html">QLCDNumber</a> and to the <tt>valueChanged()</tt> signal of the <tt>LCDRange</tt>.</p>
 
77
<p>Thus, when the signal is emitted, <tt>LCDRange</tt> emits its own <tt>valueChanged()</tt> signal. In addition, <a href="qlcdnumber.html#intValue-prop">QLCDNumber::display</a>() is called and shows the new number.</p>
 
78
<p>Note that you're not guaranteed any particular order of execution; <tt>LCDRange::valueChanged()</tt> may be emitted before or after <a href="qlcdnumber.html#intValue-prop">QLCDNumber::display</a>() is called.</p>
 
79
<pre>&nbsp;   int LCDRange::value() const
 
80
    {
 
81
        return slider-&gt;value();
 
82
    }</pre>
 
83
<p>The implementation of <tt>value()</tt> is straightforward. It simply returns the slider's value.</p>
 
84
<pre>&nbsp;   void LCDRange::setValue(int value)
 
85
    {
 
86
        slider-&gt;setValue(value);
 
87
    }</pre>
 
88
<p>The implementation of <tt>setValue()</tt> is equally straightforward. Note that because the slider and LCD number are connected, setting the slider's value automatically updates the LCD number as well. In addition, the slider will automatically adjust the value if it is outside its legal range.</p>
 
89
<a name="t7-main-cpp"></a>
 
90
<h3><a href="tutorial-t7-main-cpp.html">t7/main.cpp</a></h3>
 
91
<pre>&nbsp;       LCDRange *previousRange = 0;
 
92
 
 
93
        for (int row = 0; row &lt; 4; ++row) {
 
94
            for (int column = 0; column &lt; 4; ++column) {
 
95
                LCDRange *lcdRange = new LCDRange;
 
96
                grid-&gt;addWidget(lcdRange, row, column);
 
97
                if (previousRange)
 
98
                    connect(lcdRange, SIGNAL(valueChanged(int)),
 
99
                            previousRange, SLOT(setValue(int)));
 
100
                previousRange = lcdRange;
 
101
            }
 
102
        }</pre>
 
103
<p>All of <tt>main.cpp</tt> is copied from the previous chapter except in the constructor for <tt>MyWidget</tt>. When we create the 16 <tt>LCDRange</tt> objects, we now connect them using the <a href="signalsandslots.html">signals and slots</a> mechanism. Each has its <tt>valueChanged()</tt> signal connected to the previous one's <tt>setValue()</tt> slot. Because <tt>LCDRange</tt> emits the <tt>valueChanged()</tt> signal when its value changes, we are here creating a chain of signals and slots.</p>
 
104
<a name="compiling"></a><a name="compiling-the-application"></a>
 
105
<h2>Compiling the Application</h2>
 
106
<p>Creating a makefile for a multi-file application is no different from creating one for a single-file application. If you've saved all the files in this example in their own directory, all you have to do is:</p>
 
107
<pre>&nbsp;   qmake -project
 
108
    qmake</pre>
 
109
<p>The first command tells <a href="qmake-manual.html#qmake">qmake</a> to create a <tt>.pro</tt> file. The second command tells it to create a (platform-specific) makefile based on the project file. You should now be able to type <tt>make</tt> (or <tt>nmake</tt> if you're using Visual Studio) to build your application.</p>
 
110
<a name="running-the-application"></a>
 
111
<h2>Running the Application</h2>
 
112
<p>On startup, the program's appearance is identical to the previous one. Try operating the slider to the bottom-right.</p>
 
113
<a name="exercises"></a>
 
114
<h2>Exercises</h2>
 
115
<p>Use the bottom-right slider to set all LCDs to 50. Then set the top half to 40 by clicking once to the left of the slider handle. Now, use the one to the left of the last one operated to set the first seven LCDs back to 50.</p>
 
116
<p>Click to the left of the handle on the bottom-right slider. What happens? Why is this the correct behavior?</p>
 
117
<p>
 
118
[Previous: <a href="tutorial-t6.html">Chapter 6</a>]
 
119
[<a href="tutorial.html">Qt Tutorial</a>]
 
120
[Next: <a href="tutorial-t8.html">Chapter 8</a>]
 
121
</p>
 
122
<p /><address><hr /><div align="center">
 
123
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
124
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
125
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
126
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
127
</tr></table></div></address></body>
 
128
</html>