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

TQt Tutorial - Chapter 2: Calling it Quits

Screenshot of tutorial two

Having created a window in Chapter 1, we will now go on to make the application tquit properly when the user tells it to.

We will also use a font that is more exciting than the default one.

/****************************************************************
**
** TQt tutorial 2
**
****************************************************************/

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


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

    TQPushButton tquit( "Quit", 0 );
    tquit.resize( 75, 30 );
    tquit.setFont( TQFont( "Times", 18, TQFont::Bold ) );

    TQObject::connect( &tquit, SIGNAL(clicked()), &a, SLOT(tquit()) );

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

Line-by-line Walkthrough

    #include <qfont.h>

Since this program uses TQFont, it needs to include qfont.h. TQt's font abstraction is rather different from the horror provided by X, and loading and using fonts has been highly optimized.

        TQPushButton tquit( "Quit", 0 );

This time, the button says "Quit" and that's exactly what the program will do when the user clicks the button. This is not a coincidence. We still pass 0 as the parent, since the button is a top-level window.

        tquit.resize( 75, 30 );

We've chosen another size for the button since the text is a bit shorter than "Hello world!". We could also have used TQFontMetrics to set right size.

        tquit.setFont( TQFont( "Times", 18, TQFont::Bold ) );

Here we choose a new font for the button, an 18-point bold font from the Times family. Note that we create the font on the spot.

It is also possible to change the default font (using TQApplication::setFont()) for the whole application.

        TQObject::connect( &tquit, SIGNAL(clicked()), &a, SLOT(tquit()) );

connect() is perhaps the most central feature of TQt. Note that connect() is a static function in TQObject. Do not confuse it with the connect() function in the socket library.

This line establishes a one-way connection between two TQt objects (objects that inherit TQObject, directly or indirectly). Every TQt object can have both signals (to send messages) and slots (to receive messages). All widgets are TQt objects. They inherit TQWidget which in turn inherits TQObject.

Here, the clicked() signal of tquit is connected to the tquit() slot of a, so that when the button is clicked, the application tquits.

The Signals and Slots documentation describes this topic in detail.

Behavior

When you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button.

(See Compiling for how to create a makefile and build the application.)

Exercises

Try to resize the window. Press the button. Oops! That connect() would seem to make some difference.

Are there any other signals in TQPushButton you can connect to tquit? Hint: The TQPushButton inherits most of its behavior from TQButton.

You're now ready for Chapter 3.

[Previous tutorial] [Next tutorial] [Main tutorial page]


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8