1
<?xml version="1.0" encoding="iso-8859-1"?>
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 -->
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" />
19
<table border="0" cellpadding="0" cellspacing="0" width="100%">
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"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <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>]
28
<h1 align="center">Qt Tutorial 6 - Building Blocks Galore!</h1>
31
<li><a href="tutorial-t6-main-cpp.html">tutorial/t6/main.cpp</a></li>
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> /****************************************************************
38
****************************************************************/
40
#include <QApplication>
41
#include <QFont>
42
#include <QGridLayout>
43
#include <QLCDNumber>
44
#include <QPushButton>
45
#include <QSlider>
46
#include <QVBoxLayout>
47
#include <QWidget>
49
class LCDRange : public QWidget
52
LCDRange(QWidget *parent = 0);
55
LCDRange::LCDRange(QWidget *parent)
58
QLCDNumber *lcd = new QLCDNumber(2);
59
QSlider *slider = new QSlider(Qt::Horizontal);
60
slider->setRange(0, 99);
61
slider->setValue(0);
62
connect(slider, SIGNAL(valueChanged(int)),
63
lcd, SLOT(display(int)));
65
QVBoxLayout *layout = new QVBoxLayout;
66
layout->addWidget(lcd);
67
layout->addWidget(slider);
71
class MyWidget : public QWidget
74
MyWidget(QWidget *parent = 0);
77
MyWidget::MyWidget(QWidget *parent)
80
QPushButton *quit = new QPushButton("Quit");
81
quit->setFont(QFont("Times", 18, QFont::Bold));
82
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
84
QGridLayout *grid = new QGridLayout;
85
for (int row = 0; row < 4; ++row) {
86
for (int column = 0; column < 4; ++column) {
87
grid->addWidget(new LCDRange, row, column);
91
QVBoxLayout *layout = new QVBoxLayout;
92
layout->addWidget(quit);
93
layout->addLayout(grid);
97
int main(int argc, char *argv[])
99
QApplication app(argc, argv);
104
<a name="line-by-line-walkthrough"></a>
105
<h2>Line by Line Walkthrough</h2>
106
<pre> class LCDRange : public QWidget
109
LCDRange(QWidget *parent = 0);
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> LCDRange::LCDRange(QWidget *parent)
115
QLCDNumber *lcd = new QLCDNumber(2);
116
QSlider *slider = new QSlider(Qt::Horizontal);
117
slider->setRange(0, 99);
118
slider->setValue(0);
119
connect(slider, SIGNAL(valueChanged(int)),
120
lcd, SLOT(display(int)));
122
QVBoxLayout *layout = new QVBoxLayout;
123
layout->addWidget(lcd);
124
layout->addWidget(slider);
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> class MyWidget : public QWidget
131
MyWidget(QWidget *parent = 0);
133
<p><tt>MyWidget</tt>, too, contains no API except a constructor.</p>
134
<pre> MyWidget::MyWidget(QWidget *parent)
137
QPushButton *quit = new QPushButton("Quit");
138
quit->setFont(QFont("Times", 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> 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> for (int row = 0; row < 4; ++row) {
144
for (int column = 0; column < 4; ++column) {
145
grid->addWidget(new LCDRange, row, column);
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>
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>
156
<p>Initialize each slider with a different/random value on startup.</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>]
162
<p /><address><hr /><div align="center">
163
<table width="100%" cellspacing="0" border="0"><tr class="address">
164
<td width="30%">Copyright © 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>