<!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 > TQt 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 Classes< / font > < / a >
| < a href = "mainclasses.html" >
< font color = "#004faf" > Main Classes< / font > < / a >
| < a href = "annotated.html" >
< font color = "#004faf" > Annotated< / font > < / a >
| < a href = "groups.html" >
< font color = "#004faf" > Grouped 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 > TQt 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 TQt application up and running.
The picture above is a snapshot of this program.
< p > < pre > /****************************************************************
**
** TQt tutorial 1
**
****************************************************************/
#include < < a href = "qapplication-h.html" > ntqapplication.h< / a > >
#include < < a href = "qpushbutton-h.html" > ntqpushbutton.h< / a > >
int main( int argc, char **argv )
{
< a href = "ntqapplication.html" > TQApplication< / a > a( argc, argv );
< a href = "ntqpushbutton.html" > TQPushButton< / a > hello( "Hello world!", 0 );
hello.< a href = "ntqwidget.html#resize" > resize< / a > ( 100, 30 );
a.< a href = "ntqapplication.html#setMainWidget" > setMainWidget< / a > ( & hello );
hello.< a href = "ntqwidget.html#show" > show< / a > ();
return a.< a href = "ntqapplication.html#exec" > exec< / a > ();
}
< / pre >
< p > < h2 > Line-by-line Walkthrough
< / h2 >
< a name = "1" > < / a > < p > < pre > #include < < a href = "qapplication-h.html" > ntqapplication.h< / a > >
< / pre >
< p > This line includes the < a href = "ntqapplication.html" > TQApplication< / a > class definition. There has to be
exactly one TQApplication object in every application that uses TQt.
TQApplication manages various application-wide resources, such as the
default font and cursor.
< p > < pre > #include < < a href = "qpushbutton-h.html" > ntqpushbutton.h< / a > >
< / pre >
< p > This line includes the < a href = "ntqpushbutton.html" > TQPushButton< / 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 > TQPushButton 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 = "ntqwidget.html" > TQWidget< / 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 = "ntqapplication.html#setStyle" > look and feel< / a > and many minor
properties of it (such as color), as well as the widget's content. A
TQPushButton can show either a text or a < a href = "ntqpixmap.html" > TQPixmap< / 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 TQt, main() only needs to perform some kind of initialization
before passing the control to the TQt 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 TQt; however, TQt needs to process these arguments (see
following).
< p > < pre > < a href = "ntqapplication.html" > TQApplication< / a > a( argc, argv );
< / pre >
< p > < tt > a< / tt > is this program's < a href = "ntqapplication.html" > TQApplication< / 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 TQt are removed from
< tt > argv< / tt > (and < tt > argc< / tt > is decremented accordingly). See the < a href = "ntqapplication.html#argv" > TQApplication::argv< / a > () documentation for details.
< p > < strong > Note:< / strong > It is essential that the TQApplication object be
created before any window-system parts of TQt are used.
< p > < pre > < a href = "ntqpushbutton.html" > TQPushButton< / a > hello( "Hello world!", 0 );
< / pre >
< p > Here, < em > after< / em > the TQApplication, 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 = "ntqwidget.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 = "ntqapplication.html#setMainWidget" > 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 do have one.
< p > < pre > < a name = "x2286" > < / a > hello.< a href = "ntqwidget.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 = "ntqapplication.html#exec" > exec< / a > ();
< / pre >
< p > This is where main() passes control to TQt, and exec() will return when
the application exits.
< p > In exec(), TQt 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 TQt is to use the < a href = "qmake-manual.html" > qmake< / a > build tool supplied with TQt. 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 TQt 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 © 2007
< a href = "troll.html" > Trolltech< / a > < td align = center > < a href = "trademarks.html" > Trademarks< / a >
< td align = right > < div align = right > TQt 3.3.8< / div >
< / table > < / div > < / address > < / body >
< / html >