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:487 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Qt Tutorial - Chapter 5: Building Blocks</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { 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 Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main 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 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 5: Building Blocks</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p> <center><img src="t5.png" alt="Screenshot of tutorial five"></center>
|
---|
36 | <p> This example shows how to create and connect together several widgets
|
---|
37 | by using signals and slots, and how to handle resize events.
|
---|
38 | <p> <pre>/****************************************************************
|
---|
39 | **
|
---|
40 | ** Qt tutorial 5
|
---|
41 | **
|
---|
42 | ****************************************************************/
|
---|
43 |
|
---|
44 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
45 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
46 | #include <<a href="qslider-h.html">qslider.h</a>>
|
---|
47 | #include <<a href="qlcdnumber-h.html">qlcdnumber.h</a>>
|
---|
48 | #include <<a href="qfont-h.html">qfont.h</a>>
|
---|
49 |
|
---|
50 | #include <<a href="qvbox-h.html">qvbox.h</a>>
|
---|
51 |
|
---|
52 | class MyWidget : public <a href="qvbox.html">QVBox</a>
|
---|
53 | {
|
---|
54 | public:
|
---|
55 | MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
|
---|
56 | };
|
---|
57 |
|
---|
58 |
|
---|
59 | <a name="f553"></a>MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
60 | : <a href="qvbox.html">QVBox</a>( parent, name )
|
---|
61 | {
|
---|
62 | <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );
|
---|
63 | quit-><a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
|
---|
64 |
|
---|
65 | <a href="qobject.html#connect">connect</a>( quit, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
66 |
|
---|
67 | <a href="qlcdnumber.html">QLCDNumber</a> *lcd = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "lcd" );
|
---|
68 |
|
---|
69 | <a href="qslider.html">QSlider</a> * slider = new <a href="qslider.html">QSlider</a>( Horizontal, this, "slider" );
|
---|
70 | slider-><a href="qrangecontrol.html#setRange">setRange</a>( 0, 99 );
|
---|
71 | slider-><a href="qslider.html#setValue">setValue</a>( 0 );
|
---|
72 |
|
---|
73 | <a href="qobject.html#connect">connect</a>( slider, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)), lcd, SLOT(<a href="qlcdnumber.html#display">display</a>(int)) );
|
---|
74 | }
|
---|
75 |
|
---|
76 | int main( int argc, char **argv )
|
---|
77 | {
|
---|
78 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
79 |
|
---|
80 | MyWidget w;
|
---|
81 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &w );
|
---|
82 | w.<a href="qwidget.html#show">show</a>();
|
---|
83 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
84 | }
|
---|
85 | </pre>
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 | <p> <h2> Line-by-line Walkthrough
|
---|
90 | </h2>
|
---|
91 | <a name="1"></a><p> <pre> #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
92 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
93 | #include <<a href="qslider-h.html">qslider.h</a>>
|
---|
94 | #include <<a href="qlcdnumber-h.html">qlcdnumber.h</a>>
|
---|
95 | #include <<a href="qfont-h.html">qfont.h</a>>
|
---|
96 |
|
---|
97 | #include <<a href="qvbox-h.html">qvbox.h</a>>
|
---|
98 | </pre>
|
---|
99 | <p> Three new include files are shown here. qslider.h and qlcdnumber.h are there
|
---|
100 | because we use two new widgets, <a href="qslider.html">QSlider</a> and <a href="qlcdnumber.html">QLCDNumber</a>. qvbox.h is
|
---|
101 | here because we use Qt's automatic layout support.
|
---|
102 | <p> <pre> class MyWidget : public <a href="qvbox.html">QVBox</a>
|
---|
103 | {
|
---|
104 | public:
|
---|
105 | MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
|
---|
106 | };
|
---|
107 | </pre>
|
---|
108 | <p> <a name="constructor"></a>
|
---|
109 | <pre> MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
110 | : <a href="qvbox.html">QVBox</a>( parent, name )
|
---|
111 | {
|
---|
112 | </pre>
|
---|
113 | <p> MyWidget is now derived from <a href="qvbox.html">QVBox</a> instead of <a href="qwidget.html">QWidget</a>. That way we use
|
---|
114 | the layout of the QVBox (which places all of its children vertically
|
---|
115 | inside itself). Resizes are now handled automatically by the QVBox and
|
---|
116 | therefore by MyWidget, too.
|
---|
117 | <p> <pre> <a href="qlcdnumber.html">QLCDNumber</a> *lcd = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "lcd" );
|
---|
118 | </pre>
|
---|
119 | <p> <tt>lcd</tt> is a QLCDNumber, a widget that displays numbers in an LCD-like
|
---|
120 | fashion. This instance is set up to display two digits and to be a child of
|
---|
121 | <em>this</em>. It is named "lcd".
|
---|
122 | <p> <pre> <a href="qslider.html">QSlider</a> * slider = new <a href="qslider.html">QSlider</a>( Horizontal, this, "slider" );
|
---|
123 | <a name="x2315"></a> slider-><a href="qrangecontrol.html#setRange">setRange</a>( 0, 99 );
|
---|
124 | <a name="x2316"></a> slider-><a href="qslider.html#setValue">setValue</a>( 0 );
|
---|
125 | </pre>
|
---|
126 | <p> <a href="qslider.html">QSlider</a> is a classical slider; the user can use the widget to drag
|
---|
127 | something to adjust an integer value in a range. Here we create a
|
---|
128 | horizontal one, set its range to 0-99 (inclusive, see the <a href="qrangecontrol.html#setRange">QSlider::setRange</a>() documentation) and its initial value to 0.
|
---|
129 | <p> <pre> <a name="x2317"></a><a name="x2314"></a> <a href="qobject.html#connect">connect</a>( slider, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)), lcd, SLOT(<a href="qlcdnumber.html#display">display</a>(int)) );
|
---|
130 | </pre>
|
---|
131 | <p> Here we use the <a href="signalsandslots.html">signal/slot mechanism</a>
|
---|
132 | to connect the slider's valueChanged() signal to the LCD number's
|
---|
133 | display() slot.
|
---|
134 | <p> Whenever the slider's value changes it broadcasts the new value by
|
---|
135 | emitting the valueChanged() signal. Because that signal is connected to
|
---|
136 | the LCD number's display() slot, the slot is called when the signal is
|
---|
137 | broadcast. Neither of the objects knows about the other. This is
|
---|
138 | essential in component programming.
|
---|
139 | <p> Slots are otherwise normal C++ member functions and follow the normal
|
---|
140 | C++ access rules.
|
---|
141 | <p> <h2> Behavior
|
---|
142 | </h2>
|
---|
143 | <a name="2"></a><p> The LCD number reflects everything you do to the slider, and the
|
---|
144 | widget handles resizing well. Notice that the LCD number widget
|
---|
145 | changes in size when the window is resized (because it can), but the
|
---|
146 | others stay about the same (because otherwise they would look stupid).
|
---|
147 | <p> (See <a href="tutorial1-01.html#compiling">Compiling</a> for how to create a
|
---|
148 | makefile and build the application.)
|
---|
149 | <p> <h2> Exercises
|
---|
150 | </h2>
|
---|
151 | <a name="3"></a><p> Try changing the LCD number to add more digits or <a href="qlcdnumber.html#setMode">to change mode.</a> You can even add four push
|
---|
152 | buttons to set the number base.
|
---|
153 | <p> You can also change the slider's range.
|
---|
154 | <p> Perhaps it would have been better to use <a href="qspinbox.html">QSpinBox</a> than a slider?
|
---|
155 | <p> Try to make the application quit when the LCD number overflows.
|
---|
156 | <p> You're now ready for <a href="tutorial1-06.html">Chapter 6.</a>
|
---|
157 | <p> [<a href="tutorial1-04.html">Previous tutorial</a>]
|
---|
158 | [<a href="tutorial1-06.html">Next tutorial</a>]
|
---|
159 | [<a href="tutorial.html">Main tutorial page</a>]
|
---|
160 | <p>
|
---|
161 | <!-- eof -->
|
---|
162 | <p><address><hr><div align=center>
|
---|
163 | <table width=100% cellspacing=0 border=0><tr>
|
---|
164 | <td>Copyright © 2007
|
---|
165 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
166 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
167 | </table></div></address></body>
|
---|
168 | </html>
|
---|