You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
qt3/doc/html/tutorial1-01.html

169 lines
7.6 KiB

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial.doc:80 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Qt Tutorial - Chapter 1: Hello, World!</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
<a href="index.html">
<font color="#004faf">Home</font></a>
| <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
| <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>
<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.
<p> <pre>/****************************************************************
**
** Qt tutorial 1
**
****************************************************************/
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
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="qwidget.html#resize">resize</a>( 100, 30 );
a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;hello );
hello.<a href="qwidget.html#show">show</a>();
return a.<a href="qapplication.html#exec">exec</a>();
}
</pre>
<p> <h2> Line-by-line Walkthrough
</h2>
<a name="1"></a><p> <pre> #include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
</pre>
<p> This line includes the <a href="qapplication.html">QApplication</a> 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.
<p> <pre> #include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
</pre>
<p> This line includes the <a href="qpushbutton.html">QPushButton</a> 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 that 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 that can process user
input and draw graphics. The programmer can change both the overall
<a href="qapplication.html#setStyle">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>.
<p> <pre> int main( int argc, char **argv )
{
</pre>
<p> The main() 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> <tt>argc</tt> is the number of command-line arguments and <tt>argv</tt> is the
array of command-line arguments. This is a C/C++ feature. It is not
specific to Qt; however, Qt needs to process these arguments (see
following).
<p> <pre> <a href="qapplication.html">QApplication</a> a( argc, argv );
</pre>
<p> <tt>a</tt> is this program's <a href="qapplication.html">QApplication</a>. Here it is created and processes
some of the command-line arguments (such as -display under X Window).
Note that all command-line arguments recognized by Qt are removed from
<tt>argv</tt> (and <tt>argc</tt> is decremented accordingly). See the <a href="qapplication.html#argv">QApplication::argv</a>() documentation for details.
<p> <strong>Note:</strong> It is essential that the QApplication object be
created before any window-system parts of Qt are used.
<p> <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 (because the constructor specifies 0 for the parent
window, inside which the button should be located).
<p> <pre> <a name="x2285"></a> hello.<a href="qwidget.html#resize">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 we accept the default value.
<p> <pre> <a name="x2284"></a> a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;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 do have one.
<p> <pre> <a name="x2286"></a> hello.<a href="qwidget.html#show">show</a>();
</pre>
<p> A widget is never visible when you create it. You must call show() to
make it visible.
<p> <pre> <a name="x2283"></a> return a.<a href="qapplication.html#exec">exec</a>();
</pre>
<p> This is where main() passes control to Qt, and 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.
<p> <pre> }
</pre>
<p> You should now try to compile and run this program.
<p> <a name="compiling"></a>
<h2> Compiling
</h2>
<a name="2"></a><p> To compile a C++ application you need to create a makefile. The
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
saved <tt>main.cpp</tt> in its own directory, all you have to do is:
<pre>
qmake -project
qmake
</pre>
<p> The first command tells <a href="qmake-manual.html">qmake</a> to
create a <tt>.pro</tt> (project) file. The second command tells it to create
a (platform-specific) makefile based on the project file. You should
now be able to type <tt>make</tt> (or <tt>nmake</tt> if you're using Visual
Studio) and then run your first Qt application!
<p> <h2> Behavior
</h2>
<a name="3"></a><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>
<a name="4"></a><p> Try to resize the window. Press the button. If you're running X
Window, try running the program with the -geometry option
(for example, <tt>-geometry 100x200+10+20</tt>).
<p> You're now ready for <a href="tutorial1-02.html">Chapter 2.</a>
<p> [<a href="tutorial1-02.html">Next tutorial</a>]
[<a href="tutorial.html">Main tutorial page</a>]
<p>
<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>