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

« back to all changes in this revision

Viewing changes to doc/html/tutorial-t5.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 5 - Building Blocks</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-t4.html" />
 
15
    <link rel="contents" href="tutorial.html" />
 
16
    <link rel="next" href="tutorial-t6.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-t4.html">Chapter 4</a>]
 
25
[<a href="tutorial.html">Qt Tutorial</a>]
 
26
[Next: <a href="tutorial-t6.html">Chapter 6</a>]
 
27
</p>
 
28
<h1 align="center">Qt Tutorial 5 - Building Blocks</h1>
 
29
<p>Files:</p>
 
30
<ul>
 
31
<li><a href="tutorial-t5-main-cpp.html">tutorial/t5/main.cpp</a></li>
 
32
</ul>
 
33
<center><img src="images/t5.png" alt="Screenshot of tutorial five" /></center><p>This example shows how to create and connect together several widgets by using signals and slots, and how to handle resizes.</p>
 
34
<pre>&nbsp;   /****************************************************************
 
35
    **
 
36
    ** Qt tutorial 5
 
37
    **
 
38
    ****************************************************************/
 
39
 
 
40
    #include &lt;QApplication&gt;
 
41
    #include &lt;QFont&gt;
 
42
    #include &lt;QLCDNumber&gt;
 
43
    #include &lt;QPushButton&gt;
 
44
    #include &lt;QSlider&gt;
 
45
    #include &lt;QVBoxLayout&gt;
 
46
    #include &lt;QWidget&gt;
 
47
 
 
48
    class MyWidget : public QWidget
 
49
    {
 
50
    public:
 
51
        MyWidget(QWidget *parent = 0);
 
52
    };
 
53
 
 
54
    MyWidget::MyWidget(QWidget *parent)
 
55
        : QWidget(parent)
 
56
    {
 
57
        QPushButton *quit = new QPushButton(&quot;Quit&quot;);
 
58
        quit-&gt;setFont(QFont(&quot;Times&quot;, 18, QFont::Bold));
 
59
 
 
60
        QLCDNumber *lcd = new QLCDNumber(2);
 
61
 
 
62
        QSlider *slider = new QSlider(Qt::Horizontal);
 
63
        slider-&gt;setRange(0, 99);
 
64
        slider-&gt;setValue(0);
 
65
 
 
66
        connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
 
67
        connect(slider, SIGNAL(valueChanged(int)),
 
68
                lcd, SLOT(display(int)));
 
69
 
 
70
        QVBoxLayout *layout = new QVBoxLayout;
 
71
        layout-&gt;addWidget(quit);
 
72
        layout-&gt;addWidget(lcd);
 
73
        layout-&gt;addWidget(slider);
 
74
        setLayout(layout);
 
75
    }
 
76
 
 
77
    int main(int argc, char *argv[])
 
78
    {
 
79
        QApplication app(argc, argv);
 
80
        MyWidget widget;
 
81
        widget.show();
 
82
        return app.exec();
 
83
    }</pre>
 
84
<a name="line-by-line-walkthrough"></a>
 
85
<h2>Line by Line Walkthrough</h2>
 
86
<pre>&nbsp;   class MyWidget : public QWidget
 
87
    {
 
88
    public:
 
89
        MyWidget(QWidget *parent = 0);
 
90
    };</pre>
 
91
<a name="constructor"></a><pre>&nbsp;   MyWidget::MyWidget(QWidget *parent)
 
92
        : QWidget(parent)
 
93
    {
 
94
        QPushButton *quit = new QPushButton(&quot;Quit&quot;);
 
95
        quit-&gt;setFont(QFont(&quot;Times&quot;, 18, QFont::Bold));
 
96
 
 
97
        QLCDNumber *lcd = new QLCDNumber(2);</pre>
 
98
<p><tt>lcd</tt> is a <a href="qlcdnumber.html">QLCDNumber</a>, a widget that displays numbers in an LCD-like fashion. This instance is set up to display two digits.</p>
 
99
<pre>&nbsp;       QSlider *slider = new QSlider(Qt::Horizontal);
 
100
        slider-&gt;setRange(0, 99);
 
101
        slider-&gt;setValue(0);</pre>
 
102
<p>The user can use the <a href="qslider.html">QSlider</a> widget to adjust an integer value in a range. Here we create a horizontal one, set its minimum value to 0, its maximum value to 99, and its initial value to 0.</p>
 
103
<pre>&nbsp;       connect(slider, SIGNAL(valueChanged(int)),
 
104
                lcd, SLOT(display(int)));</pre>
 
105
<p>Here we use the <a href="signalsandslots.html">signals and slots</a> mechanism to connect the slider's <a href="qabstractslider.html#valueChanged">valueChanged()</a> signal to the LCD number's <a href="qlcdnumber.html#intValue-prop">display()</a> slot.</p>
 
106
<p>Whenever the slider's value changes it broadcasts the new value by emitting the <a href="qabstractslider.html#valueChanged">valueChanged()</a> signal. Because that signal is connected to the LCD number's <a href="qlcdnumber.html#intValue-prop">display()</a> slot, the slot is called when the signal is broadcast. Neither of the objects knows about the other. This is essential in component programming.</p>
 
107
<p>Slots are otherwise normal C++ member functions and follow the normal C++ access rules.</p>
 
108
<pre>&nbsp;       QVBoxLayout *layout = new QVBoxLayout;
 
109
        layout-&gt;addWidget(quit);
 
110
        layout-&gt;addWidget(lcd);
 
111
        layout-&gt;addWidget(slider);
 
112
        setLayout(layout);</pre>
 
113
<p><tt>MyWidget</tt> now uses a <a href="qvboxlayout.html">QVBoxLayout</a> to manage the geometry of its child widgets. For that reason, we don't need to specify the screen coordinates for each widget like we did in Chapter 4. In addition, using a layout ensures that the child widgets are resized when the window is resized. Then we add the <tt>quit</tt>, <tt>lcd</tt> and <tt>slider</tt> widgets to the layout using <a href="qboxlayout.html#addWidget">QBoxLayout::addWidget</a>().</p>
 
114
<p>The <a href="qwidget.html#setLayout">QWidget::setLayout</a>() function installs the layout on <tt>MyWidget</tt>. This makes the layout a child widget of <tt>MyWidget</tt> so we don't have to worry about deleting it; it will be deleted together with <tt>MyWidget</tt>. Also, the call to <a href="qwidget.html#setLayout">QWidget::setLayout</a>() automatically reparents the widgets in the layout so that they are children of <tt>MyWidget</tt>. Because of this, we didn't need to specify <tt>this</tt> as the parent for the <tt>quit</tt>, <tt>lcd</tt> and <tt>slider</tt> widgets.</p>
 
115
<a name="running-the-application"></a>
 
116
<h2>Running the Application</h2>
 
117
<p>The LCD number reflects everything you do to the slider, and the widget handles resizing well. Notice that the LCD number widget changes in size when the window is resized (because it can), but the others stay about the same (because otherwise they would look strange).</p>
 
118
<a name="exercises"></a>
 
119
<h2>Exercises</h2>
 
120
<p>Try changing the LCD number to add more digits or to change mode (<a href="qlcdnumber.html#mode-prop">QLCDNumber::setMode</a>()). You can even add four push buttons to set the number base.</p>
 
121
<p>You can also change the slider's range.</p>
 
122
<p>Perhaps it would have been better to use <a href="qspinbox.html">QSpinBox</a> than a slider?</p>
 
123
<p>Try to make the application quit when the LCD number overflows.</p>
 
124
<p>
 
125
[Previous: <a href="tutorial-t4.html">Chapter 4</a>]
 
126
[<a href="tutorial.html">Qt Tutorial</a>]
 
127
[Next: <a href="tutorial-t6.html">Chapter 6</a>]
 
128
</p>
 
129
<p /><address><hr /><div align="center">
 
130
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
131
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
132
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
133
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
134
</tr></table></div></address></body>
 
135
</html>