source: trunk/doc/html/tutorial1-04.html@ 203

Last change on this file since 203 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 8.6 KB
Line 
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:378 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Qt Tutorial - Chapter 4: Let There Be Widgets</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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 4: Let There Be Widgets</h1>
33
34
35<p> <center><img src="t4.png" alt="Screenshot of tutorial four"></center>
36<p> This example shows how to create your own widget, describes how to control the
37minimum and maximum sizes of a widget, and introduces widget names.
38<p> <pre>/****************************************************************
39**
40** Qt tutorial 4
41**
42****************************************************************/
43
44#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
45#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
46#include &lt;<a href="qfont-h.html">qfont.h</a>&gt;
47
48
49class MyWidget : public <a href="qwidget.html">QWidget</a>
50{
51public:
52 MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
53};
54
55
56<a name="f552"></a>MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
57 : <a href="qwidget.html">QWidget</a>( parent, name )
58{
59 <a href="qwidget.html#setMinimumSize">setMinimumSize</a>( 200, 120 );
60 <a href="qwidget.html#setMaximumSize">setMaximumSize</a>( 200, 120 );
61
62 <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );
63 quit-&gt;<a href="qwidget.html#setGeometry">setGeometry</a>( 62, 40, 75, 30 );
64 quit-&gt;<a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
65
66 <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>()) );
67}
68
69
70int main( int argc, char **argv )
71{
72 <a href="qapplication.html">QApplication</a> a( argc, argv );
73
74 MyWidget w;
75 w.<a href="qwidget.html#setGeometry">setGeometry</a>( 100, 100, 200, 120 );
76 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;w );
77 w.<a href="qwidget.html#show">show</a>();
78 return a.<a href="qapplication.html#exec">exec</a>();
79}
80</pre>
81
82
83
84<p> <h2> Line-by-line Walkthrough
85</h2>
86<a name="1"></a><p> <pre> class MyWidget : public <a href="qwidget.html">QWidget</a>
87 {
88 public:
89 MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
90 };
91</pre>
92<p> Here we create a new class. Because this class inherits from <a href="qwidget.html">QWidget</a>,
93the new class is a widget and may be a top level window or a child
94widget (like the push button in Chapter 3).
95<p> This class has only one member, a constructor (in addition to the
96members it inherits from QWidget). The constructor is a standard Qt
97widget constructor; you should always include a similar constructor
98when you create widgets.
99<p> The first argument is its parent widget. To create a top-level window
100you specify a null pointer as the parent. As you can see, this widget
101defaults to be a top-level window.
102<p> The second argument is the widget's name. This is <em>not</em> the text
103that appears in the window's title bar or in the button. It is a name
104associated with a widget to make it possible to <a href="qobject.html#queryList">look up</a> this widget later, and there is
105also a <a href="qobject.html#dumpObjectTree">handy debugging function</a> that will list a complete widget hierarchy.
106<p> <pre> MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
107 : <a href="qwidget.html">QWidget</a>( parent, name )
108</pre>
109<p> The implementation of the constructor starts here. Like most widgets,
110it just passes on the <tt>parent</tt> and <tt>name</tt> to the <a href="qwidget.html">QWidget</a>
111constructor.
112<p> <pre> {
113 <a href="qwidget.html#setMinimumSize">setMinimumSize</a>( 200, 120 );
114 <a href="qwidget.html#setMaximumSize">setMaximumSize</a>( 200, 120 );
115</pre>
116<p> Because this widget doesn't know how to handle resizing, we fix its size
117by setting the minimum and maximum to be equal. In the next chapter
118we will show how a widget can respond to resize event from the user.
119<p> <pre> <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );
120 <a name="x2308"></a> quit-&gt;<a href="qwidget.html#setGeometry">setGeometry</a>( 62, 40, 75, 30 );
121 <a name="x2307"></a> quit-&gt;<a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
122</pre>
123<p> Here we create and set up a child widget of this widget (the new widget's
124parent is <tt>this</tt>) which has the widget name "quit". The widget
125name has nothing to do with the button text; it just happens to be
126similar in this case.
127<p> Note that <tt>quit</tt> is a local variable in the constructor. MyWidget
128does not keep track of it, but Qt does, and will by default delete it
129when MyWidget is deleted. This is why MyWidget doesn't need a
130destructor. (On the other hand, there is no harm in deleting a child
131when you choose to, the child will automatically tell Qt about its
132imminent death.)
133<p> The setGeometry() call does the same as move() and resize() did in the
134previous chapters.
135<p> <pre> <a name="x2306"></a><a name="x2304"></a> <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>()) );
136 }
137</pre>
138<p> Because the MyWidget class doesn't know about the application object, it
139has to connect to Qt's pointer to it, <tt>qApp</tt>.
140<p> A widget is a software component and should know as little as possible
141about its environment in order to be as general and reusable as
142possible.
143<p> Knowing the name of the application object would break this principle,
144so Qt offers an alias, qApp, for the cases in which a component such as
145MyWidget needs to talk to the application object.
146<p> <pre> int main( int argc, char **argv )
147 {
148 <a href="qapplication.html">QApplication</a> a( argc, argv );
149
150 MyWidget w;
151 w.<a href="qwidget.html#setGeometry">setGeometry</a>( 100, 100, 200, 120 );
152 <a name="x2305"></a> a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;w );
153 <a name="x2309"></a> w.<a href="qwidget.html#show">show</a>();
154 <a name="x2303"></a> return a.<a href="qapplication.html#exec">exec</a>();
155 }
156</pre>
157<p> Here we instantiate our new child, set it to be the main widget, and
158execute the application.
159<p> <h2> Behavior
160</h2>
161<a name="2"></a><p> This program is very similar in behavior to the previous one. The
162difference lies in the way we have implemented it. It does behave
163slightly differently, however. Just try to resize it to see.
164<p> (See <a href="tutorial1-01.html#compiling">Compiling</a> for how to create a
165makefile and build the application.)
166<p> <h2> Exercises
167</h2>
168<a name="3"></a><p> Try to create another MyWidget object in main(). What happens?
169<p> Try to add more buttons or put in widgets other than <a href="qpushbutton.html">QPushButton</a>.
170<p> You're now ready for <a href="tutorial1-05.html">Chapter 5.</a>
171<p> [<a href="tutorial1-03.html">Previous tutorial</a>]
172[<a href="tutorial1-05.html">Next tutorial</a>]
173[<a href="tutorial.html">Main tutorial page</a>]
174<p>
175<!-- eof -->
176<p><address><hr><div align=center>
177<table width=100% cellspacing=0 border=0><tr>
178<td>Copyright &copy; 2007
179<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
180<td align=right><div align=right>Qt 3.3.8</div>
181</table></div></address></body>
182</html>
Note: See TracBrowser for help on using the repository browser.