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

« back to all changes in this revision

Viewing changes to doc/html/tutorial-t6.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 6 - Building Blocks Galore!</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-t5.html" />
 
15
    <link rel="contents" href="tutorial.html" />
 
16
    <link rel="next" href="tutorial-t7.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-t5.html">Chapter 5</a>]
 
25
[<a href="tutorial.html">Qt Tutorial</a>]
 
26
[Next: <a href="tutorial-t7.html">Chapter 7</a>]
 
27
</p>
 
28
<h1 align="center">Qt Tutorial 6 - Building Blocks Galore!</h1>
 
29
<p>Files:</p>
 
30
<ul>
 
31
<li><a href="tutorial-t6-main-cpp.html">tutorial/t6/main.cpp</a></li>
 
32
</ul>
 
33
<center><img src="images/t6.png" alt="Screenshot of tutorial six" /></center><p>This example shows how to encapsulate two widgets into a new component and how easy it is to use many widgets. For the first time, we use a custom widget as a child widget.</p>
 
34
<pre>&nbsp;   /****************************************************************
 
35
    **
 
36
    ** Qt tutorial 6
 
37
    **
 
38
    ****************************************************************/
 
39
 
 
40
    #include &lt;QApplication&gt;
 
41
    #include &lt;QFont&gt;
 
42
    #include &lt;QGridLayout&gt;
 
43
    #include &lt;QLCDNumber&gt;
 
44
    #include &lt;QPushButton&gt;
 
45
    #include &lt;QSlider&gt;
 
46
    #include &lt;QVBoxLayout&gt;
 
47
    #include &lt;QWidget&gt;
 
48
 
 
49
    class LCDRange : public QWidget
 
50
    {
 
51
    public:
 
52
        LCDRange(QWidget *parent = 0);
 
53
    };
 
54
 
 
55
    LCDRange::LCDRange(QWidget *parent)
 
56
        : QWidget(parent)
 
57
    {
 
58
        QLCDNumber *lcd = new QLCDNumber(2);
 
59
        QSlider *slider = new QSlider(Qt::Horizontal);
 
60
        slider-&gt;setRange(0, 99);
 
61
        slider-&gt;setValue(0);
 
62
        connect(slider, SIGNAL(valueChanged(int)),
 
63
                lcd, SLOT(display(int)));
 
64
 
 
65
        QVBoxLayout *layout = new QVBoxLayout;
 
66
        layout-&gt;addWidget(lcd);
 
67
        layout-&gt;addWidget(slider);
 
68
        setLayout(layout);
 
69
    }
 
70
 
 
71
    class MyWidget : public QWidget
 
72
    {
 
73
    public:
 
74
        MyWidget(QWidget *parent = 0);
 
75
    };
 
76
 
 
77
    MyWidget::MyWidget(QWidget *parent)
 
78
        : QWidget(parent)
 
79
    {
 
80
        QPushButton *quit = new QPushButton(&quot;Quit&quot;);
 
81
        quit-&gt;setFont(QFont(&quot;Times&quot;, 18, QFont::Bold));
 
82
        connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
 
83
 
 
84
        QGridLayout *grid = new QGridLayout;
 
85
        for (int row = 0; row &lt; 4; ++row) {
 
86
            for (int column = 0; column &lt; 4; ++column) {
 
87
                grid-&gt;addWidget(new LCDRange, row, column);
 
88
            }
 
89
        }
 
90
 
 
91
        QVBoxLayout *layout = new QVBoxLayout;
 
92
        layout-&gt;addWidget(quit);
 
93
        layout-&gt;addLayout(grid);
 
94
        setLayout(layout);
 
95
    }
 
96
 
 
97
    int main(int argc, char *argv[])
 
98
    {
 
99
        QApplication app(argc, argv);
 
100
        MyWidget widget;
 
101
        widget.show();
 
102
        return app.exec();
 
103
    }</pre>
 
104
<a name="line-by-line-walkthrough"></a>
 
105
<h2>Line by Line Walkthrough</h2>
 
106
<pre>&nbsp;   class LCDRange : public QWidget
 
107
    {
 
108
    public:
 
109
        LCDRange(QWidget *parent = 0);
 
110
    };</pre>
 
111
<p>The <tt>LCDRange</tt> widget is a widget without any API. It just has a constructor. This sort of widget is not very useful, so we'll add some API later.</p>
 
112
<pre>&nbsp;   LCDRange::LCDRange(QWidget *parent)
 
113
        : QWidget(parent)
 
114
    {
 
115
        QLCDNumber *lcd = new QLCDNumber(2);
 
116
        QSlider *slider = new QSlider(Qt::Horizontal);
 
117
        slider-&gt;setRange(0, 99);
 
118
        slider-&gt;setValue(0);
 
119
        connect(slider, SIGNAL(valueChanged(int)),
 
120
                lcd, SLOT(display(int)));
 
121
 
 
122
        QVBoxLayout *layout = new QVBoxLayout;
 
123
        layout-&gt;addWidget(lcd);
 
124
        layout-&gt;addWidget(slider);
 
125
        setLayout(layout);
 
126
    }</pre>
 
127
<p>This is lifted straight from the <tt>MyWidget</tt> constructor in Chapter 5. The only differences are that the <b>Quit</b> button is left out and the class is renamed.</p>
 
128
<pre>&nbsp;   class MyWidget : public QWidget
 
129
    {
 
130
    public:
 
131
        MyWidget(QWidget *parent = 0);
 
132
    };</pre>
 
133
<p><tt>MyWidget</tt>, too, contains no API except a constructor.</p>
 
134
<pre>&nbsp;   MyWidget::MyWidget(QWidget *parent)
 
135
        : QWidget(parent)
 
136
    {
 
137
        QPushButton *quit = new QPushButton(&quot;Quit&quot;);
 
138
        quit-&gt;setFont(QFont(&quot;Times&quot;, 18, QFont::Bold));
 
139
        connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));</pre>
 
140
<p>The push button that used to be in what is now <tt>LCDRange</tt> has been separated so that we can have one <b>Quit</b> button and many <tt>LCDRange</tt> objects.</p>
 
141
<pre>&nbsp;       QGridLayout *grid = new QGridLayout;</pre>
 
142
<p>We create a <a href="qwidget.html">QWidget</a> with a <a href="qgridlayout.html">QGridLayout</a> that will contain four columns. The <a href="qgridlayout.html">QGridLayout</a> automatically arranges its widgets in rows and columns; you can specify the row and column numbers when adding widgets to the layout, and <a href="qgridlayout.html">QGridLayout</a> will fit them into the grid.</p>
 
143
<pre>&nbsp;       for (int row = 0; row &lt; 4; ++row) {
 
144
            for (int column = 0; column &lt; 4; ++column) {
 
145
                grid-&gt;addWidget(new LCDRange, row, column);
 
146
            }
 
147
        }</pre>
 
148
<p>We create 16 <tt>LCDRange</tt> widgets, all of which are children of the grid object. The <a href="qgridlayout.html">QGridLayout</a> will arrange them in four columns.</p>
 
149
<pre>&nbsp;   }</pre>
 
150
<p>That's all.</p>
 
151
<a name="running-the-application"></a>
 
152
<h2>Running the Application</h2>
 
153
<p>This program shows how easy it is to use many widgets at a time. Each one behaves like the slider and LCD number in the previous chapter. Again, the difference lies in the implementation.</p>
 
154
<a name="exercises"></a>
 
155
<h2>Exercises</h2>
 
156
<p>Initialize each slider with a different/random value on startup.</p>
 
157
<p>
 
158
[Previous: <a href="tutorial-t5.html">Chapter 5</a>]
 
159
[<a href="tutorial.html">Qt Tutorial</a>]
 
160
[Next: <a href="tutorial-t7.html">Chapter 7</a>]
 
161
</p>
 
162
<p /><address><hr /><div align="center">
 
163
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
164
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
165
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
166
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
167
</tr></table></div></address></body>
 
168
</html>