1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>
Qt Tutorial - Chapter 1: Hello, World!
</title></head><body bgcolor="#ffffff">
<p>
<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>
<p>
<h1 align=center>Chapter 1: Hello, World!</h1><br clear="all">
<p>
<center><img src="t1.png" alt="Screenshot of tutorial one"></center>
<p>
This first program is a simple hello-world example. It contains only
the bare minimum you need to get a Qt application up and running.
The picture above is a snapshot of this program. <pre>/****************************************************************
**
** Qt tutorial 1
**
****************************************************************/
#include <<a href="qapplication-h.html">qapplication.h</a>>
#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
int main( int argc, char **argv )
{
<a href="qapplication.html">QApplication</a> a( argc, argv );
<a href="qpushbutton.html">QPushButton</a> hello( "Hello world!", 0 );
hello.<a href="qpushbutton.html#13fb8e">resize</a>( 100, 30 );
a.<a href="qapplication.html#7ad759">setMainWidget</a>( &hello );
hello.<a href="qwidget.html#200ee5">show</a>();
return a.<a href="qapplication.html#84c7bf">exec</a>();
}
</pre>
<p>
<h2>Line by Line Walk-Through</h2> <pre>
#include <<a href="qapplication-h.html">qapplication.h</a>>
</pre>
<p>
This line includes the QApplication class definition. There has to be
exactly one QApplication object in every application that uses Qt.
QApplication manages various application-wide resources, such as the
default font and cursor. <pre>
#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
</pre>
<p>
This line includes the QPushButton class definition. The <a
href="hierarchy.html">reference documentation</a> for each class
mentions at the top which file needs to be included to use that class.
<p>
QPushButton is a classical GUI push button, which the user can press
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 which can process user
input and draw graphics. The programmer can change both the overall
<a href="qapplication.html#e52522">look and feel</a> and many minor
properties of it such as color, as well as the widget's content. A
QPushButton can show either a text or a <a href="qpixmap.html">QPixmap</a>. <pre>
int main( int argc, char **argv )
{
</pre>
<p>
The <code>main()</code> function is the entry point to the program. Almost always
when using Qt, main() only needs to perform some kind of initialization
before passing the control to the Qt library, which then tells the
program about the user's actions via events.
<p>
<code>argc</code> is the number of command-line arguments and <code>argv</code> is the array
of command-line arguments. This is a C/C++ feature. It is not Qt specific,
however, Qt needs to process these arguments (see below). <pre>
<a href="qapplication.html">QApplication</a> a( argc, argv );
</pre>
<p>
<code>a</code> is this program's QApplication. Here it is created and processes
some of the command-line arguments (such as -display under X11).
Note that all command-line arguments recognized by Qt are removed from
<code>argv</code> (and <code>argc</code> is decremented accordingly). See the <a href="qapplication.html#c35457">QApplication::argv()</a> documentation for
details.
<p>
<strong>Note:</strong> It is essential that the QApplication object is
created before any window-system parts of Qt are used. <pre>
<a href="qpushbutton.html">QPushButton</a> hello( "Hello world!", 0 );
</pre>
<p>
Here, <em>after</em> the QApplication, comes the first window-system code: A
push button is created.
<p>
The button is set up to display the text "Hello world!" and be a
window of its own (since the constructor specifies 0 for the parent
window which the button should be inside). <pre>
hello.<a href="qpushbutton.html#13fb8e">resize</a>( 100, 30 );
</pre>
<p>
The button is set up to be 100 pixels wide and 30 pixels high (plus the
window system frame). In this case we don't care about the button's
position and accept the default value. <pre>
a.<a href="qapplication.html#7ad759">setMainWidget</a>( &hello );
</pre>
<p>
The push button is chosen as the main widget for the application. If
the user closes a main widget, the application exits.
<p>
You don't have to have a main widget, but most programs have one. <pre>
hello.<a href="qwidget.html#200ee5">show</a>();
</pre>
<p>
A widget is never visible when you create it. You must call show() to
make it visible. <pre>
return a.<a href="qapplication.html#84c7bf">exec</a>();
</pre>
<p>
This is where <code>main()</code> passes control to Qt. exec() will return when
the application exits.
<p>
In exec(), Qt receives and processes user and system events and passes
these on to the appropriate widgets. <pre>
}
</pre>
<p>
<h2>Behavior</h2>
<p>
You should now try to compile and run this program.
<p>
When you run it, you will see a small window filled with a single
button, and on it you can read the famous words, Hello World!
<p>
<h2>Exercises</h2>
<p>
Try to resize the window. Press the button. If you're running X11,
try running the program with the -geometry option
(e.g. -geometry 100x200+10+20)
<p>
You may now go on to <a href="t2.html">chapter two.</a>
<p>
[<a href="t2.html">Next tutorial</a>]
[<a href="tutorial.html">Main tutorial page</a>]
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright 2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>
|