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"><!--
|
---|
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 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
|
---|
37 | the bare minimum you need to get a Qt application up and running.
|
---|
38 | The picture above is a snapshot of this program.
|
---|
39 | <p> <pre>/****************************************************************
|
---|
40 | **
|
---|
41 | ** Qt tutorial 1
|
---|
42 | **
|
---|
43 | ****************************************************************/
|
---|
44 |
|
---|
45 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
46 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
47 |
|
---|
48 |
|
---|
49 | int 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>( &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 <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
67 | </pre>
|
---|
68 | <p> This line includes the <a href="qapplication.html">QApplication</a> class definition. There has to be
|
---|
69 | exactly one QApplication object in every application that uses Qt.
|
---|
70 | QApplication manages various application-wide resources, such as the
|
---|
71 | default font and cursor.
|
---|
72 | <p> <pre> #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
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
|
---|
76 | mentions 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
|
---|
78 | and 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
|
---|
79 | input and draw graphics. The programmer can change both the overall
|
---|
80 | <a href="qapplication.html#setStyle">look and feel</a> and many minor
|
---|
81 | properties of it (such as color), as well as the widget's content. A
|
---|
82 | QPushButton 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
|
---|
87 | when using Qt, main() only needs to perform some kind of initialization
|
---|
88 | before passing the control to the Qt library, which then tells the
|
---|
89 | program 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
|
---|
91 | array of command-line arguments. This is a C/C++ feature. It is not
|
---|
92 | specific to Qt; however, Qt needs to process these arguments (see
|
---|
93 | following).
|
---|
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
|
---|
97 | some of the command-line arguments (such as -display under X Window).
|
---|
98 | Note 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
|
---|
101 | created 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
|
---|
105 | push button is created.
|
---|
106 | <p> The button is set up to display the text "Hello world!" and be a
|
---|
107 | window of its own (because the constructor specifies 0 for the parent
|
---|
108 | window, 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
|
---|
112 | window system frame). In this case we don't care about the button's
|
---|
113 | position, and we accept the default value.
|
---|
114 | <p> <pre> <a name="x2284"></a> a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &hello );
|
---|
115 | </pre>
|
---|
116 | <p> The push button is chosen as the main widget for the application. If
|
---|
117 | the 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
|
---|
122 | make 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
|
---|
126 | the application exits.
|
---|
127 | <p> In exec(), Qt receives and processes user and system events and passes
|
---|
128 | these 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
|
---|
136 | easiest 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
|
---|
137 | saved <tt>main.cpp</tt> in its own directory, all you have to do is:
|
---|
138 | <pre>
|
---|
139 | qmake -project
|
---|
140 | qmake
|
---|
141 | </pre>
|
---|
142 |
|
---|
143 | <p> The first command tells <a href="qmake-manual.html">qmake</a> to
|
---|
144 | create a <tt>.pro</tt> (project) file. The second command tells it to create
|
---|
145 | a (platform-specific) makefile based on the project file. You should
|
---|
146 | now be able to type <tt>make</tt> (or <tt>nmake</tt> if you're using Visual
|
---|
147 | Studio) 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
|
---|
151 | button, 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
|
---|
155 | Window, 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 © 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>
|
---|