Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

TQt Tutorial - Chapter 1: Hello, World!

Screenshot of tutorial one

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.

/****************************************************************
**
** TQt tutorial 1
**
****************************************************************/

#include <qapplication.h>
#include <qpushbutton.h>


int main( int argc, char **argv )
{
    TQApplication a( argc, argv );

    TQPushButton hello( "Hello world!", 0 );
    hello.resize( 100, 30 );

    a.setMainWidget( &hello );
    hello.show();
    return a.exec();
}

Line-by-line Walkthrough

    #include <qapplication.h>

This line includes the TQApplication 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.

    #include <qpushbutton.h>

This line includes the TQPushButton class definition. The reference documentation for each class mentions at the top which file needs to be included to use that class.

TQPushButton is a classical GUI push button that the user can press and release. It manages its own look and feel, like every other TQWidget. A widget is a user interface object that can process user input and draw graphics. The programmer can change both the overall look and feel 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 TQPixmap.

    int main( int argc, char **argv )
    {

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.

argc is the number of command-line arguments and argv 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).

        TQApplication a( argc, argv );

a is this program's TQApplication. 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 argv (and argc is decremented accordingly). See the TQApplication::argv() documentation for details.

Note: It is essential that the TQApplication object be created before any window-system parts of TQt are used.

        TQPushButton hello( "Hello world!", 0 );

Here, after the TQApplication, comes the first window-system code: A push button is created.

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).

        hello.resize( 100, 30 );

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.

        a.setMainWidget( &hello );

The push button is chosen as the main widget for the application. If the user closes a main widget, the application exits.

You don't have to have a main widget, but most programs do have one.

        hello.show();

A widget is never visible when you create it. You must call show() to make it visible.

        return a.exec();

This is where main() passes control to TQt, and exec() will return when the application exits.

In exec(), TQt receives and processes user and system events and passes these on to the appropriate widgets.

    }

You should now try to compile and run this program.

Compiling

To compile a C++ application you need to create a makefile. The easiest way to create a makefile for TQt is to use the qmake build tool supplied with TQt. If you've saved main.cpp in its own directory, all you have to do is:

qmake -project
qmake

The first command tells qmake to create a .pro (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 make (or nmake if you're using Visual Studio) and then run your first TQt application!

Behavior

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!

Exercises

Try to resize the window. Press the button. If you're running X Window, try running the program with the -geometry option (for example, -geometry 100x200+10+20).

You're now ready for Chapter 2.

[Next tutorial] [Main tutorial page]


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8