source: trunk/doc/html/tutorial1-01.html@ 190

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

reference documentation added

File size: 7.8 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:80 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Qt Tutorial - Chapter 1: Hello, World!</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 1: Hello, World!</h1>
33
34
35<p> <center><img src="t1.png" alt="Screenshot of tutorial one"></center>
36<p> This first program is a simple hello-world example. It contains only
37the bare minimum you need to get a Qt application up and running.
38The picture above is a snapshot of this program.
39<p> <pre>/****************************************************************
40**
41** Qt tutorial 1
42**
43****************************************************************/
44
45#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
46#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
47
48
49int main( int argc, char **argv )
50{
51 <a href="qapplication.html">QApplication</a> a( argc, argv );
52
53 <a href="qpushbutton.html">QPushButton</a> hello( "Hello world!", 0 );
54 hello.<a href="qwidget.html#resize">resize</a>( 100, 30 );
55
56 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;hello );
57 hello.<a href="qwidget.html#show">show</a>();
58 return a.<a href="qapplication.html#exec">exec</a>();
59}
60</pre>
61
62
63
64<p> <h2> Line-by-line Walkthrough
65</h2>
66<a name="1"></a><p> <pre> #include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
67</pre>
68<p> This line includes the <a href="qapplication.html">QApplication</a> class definition. There has to be
69exactly one QApplication object in every application that uses Qt.
70QApplication manages various application-wide resources, such as the
71default font and cursor.
72<p> <pre> #include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
73</pre>
74<p> This line includes the <a href="qpushbutton.html">QPushButton</a> class definition. The
75<a href="hierarchy.html">reference documentation</a> for each class
76mentions at the top which file needs to be included to use that class.
77<p> QPushButton is a classical GUI push button that the user can press
78and release. It manages its own look and feel, like every other <a href="qwidget.html">QWidget</a>. A widget is a user interface object that can process user
79input and draw graphics. The programmer can change both the overall
80<a href="qapplication.html#setStyle">look and feel</a> and many minor
81properties of it (such as color), as well as the widget's content. A
82QPushButton can show either a text or a <a href="qpixmap.html">QPixmap</a>.
83<p> <pre> int main( int argc, char **argv )
84 {
85</pre>
86<p> The main() function is the entry point to the program. Almost always
87when using Qt, main() only needs to perform some kind of initialization
88before passing the control to the Qt library, which then tells the
89program about the user's actions via events.
90<p> <tt>argc</tt> is the number of command-line arguments and <tt>argv</tt> is the
91array of command-line arguments. This is a C/C++ feature. It is not
92specific to Qt; however, Qt needs to process these arguments (see
93following).
94<p> <pre> <a href="qapplication.html">QApplication</a> a( argc, argv );
95</pre>
96<p> <tt>a</tt> is this program's <a href="qapplication.html">QApplication</a>. Here it is created and processes
97some of the command-line arguments (such as -display under X Window).
98Note that all command-line arguments recognized by Qt are removed from
99<tt>argv</tt> (and <tt>argc</tt> is decremented accordingly). See the <a href="qapplication.html#argv">QApplication::argv</a>() documentation for details.
100<p> <strong>Note:</strong> It is essential that the QApplication object be
101created before any window-system parts of Qt are used.
102<p> <pre> <a href="qpushbutton.html">QPushButton</a> hello( "Hello world!", 0 );
103</pre>
104<p> Here, <em>after</em> the QApplication, comes the first window-system code: A
105push button is created.
106<p> The button is set up to display the text "Hello world!" and be a
107window of its own (because the constructor specifies 0 for the parent
108window, inside which the button should be located).
109<p> <pre> <a name="x2285"></a> hello.<a href="qwidget.html#resize">resize</a>( 100, 30 );
110</pre>
111<p> The button is set up to be 100 pixels wide and 30 pixels high (plus the
112window system frame). In this case we don't care about the button's
113position, and we accept the default value.
114<p> <pre> <a name="x2284"></a> a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;hello );
115</pre>
116<p> The push button is chosen as the main widget for the application. If
117the user closes a main widget, the application exits.
118<p> You don't have to have a main widget, but most programs do have one.
119<p> <pre> <a name="x2286"></a> hello.<a href="qwidget.html#show">show</a>();
120</pre>
121<p> A widget is never visible when you create it. You must call show() to
122make it visible.
123<p> <pre> <a name="x2283"></a> return a.<a href="qapplication.html#exec">exec</a>();
124</pre>
125<p> This is where main() passes control to Qt, and exec() will return when
126the application exits.
127<p> In exec(), Qt receives and processes user and system events and passes
128these on to the appropriate widgets.
129<p> <pre> }
130</pre>
131<p> You should now try to compile and run this program.
132<p> <a name="compiling"></a>
133<h2> Compiling
134</h2>
135<a name="2"></a><p> To compile a C++ application you need to create a makefile. The
136easiest way to create a makefile for Qt is to use the <a href="qmake-manual.html">qmake</a> build tool supplied with Qt. If you've
137saved <tt>main.cpp</tt> in its own directory, all you have to do is:
138<pre>
139qmake -project
140qmake
141</pre>
142
143<p> The first command tells <a href="qmake-manual.html">qmake</a> to
144create a <tt>.pro</tt> (project) file. The second command tells it to create
145a (platform-specific) makefile based on the project file. You should
146now be able to type <tt>make</tt> (or <tt>nmake</tt> if you're using Visual
147Studio) and then run your first Qt application!
148<p> <h2> Behavior
149</h2>
150<a name="3"></a><p> When you run it, you will see a small window filled with a single
151button, and on it you can read the famous words, Hello World!
152<p> <h2> Exercises
153</h2>
154<a name="4"></a><p> Try to resize the window. Press the button. If you're running X
155Window, try running the program with the -geometry option
156(for example, <tt>-geometry 100x200+10+20</tt>).
157<p> You're now ready for <a href="tutorial1-02.html">Chapter 2.</a>
158<p> [<a href="tutorial1-02.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 &copy; 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>
Note: See TracBrowser for help on using the repository browser.