diff options
Diffstat (limited to 'examples/hello')
-rw-r--r-- | examples/hello/README | 3 | ||||
-rw-r--r-- | examples/hello/hello.cpp | 101 | ||||
-rw-r--r-- | examples/hello/hello.doc | 30 | ||||
-rw-r--r-- | examples/hello/hello.h | 33 | ||||
-rw-r--r-- | examples/hello/hello.pro | 9 | ||||
-rw-r--r-- | examples/hello/main.cpp | 40 |
6 files changed, 216 insertions, 0 deletions
diff --git a/examples/hello/README b/examples/hello/README new file mode 100644 index 000000000..1d9560106 --- /dev/null +++ b/examples/hello/README @@ -0,0 +1,3 @@ +Since the classic "hello world" program is too trivial in Qt, We made +this one - it draws the words "hello world" moving around and in +rainbow colors. diff --git a/examples/hello/hello.cpp b/examples/hello/hello.cpp new file mode 100644 index 000000000..55f8356e0 --- /dev/null +++ b/examples/hello/hello.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "hello.h" +#include <qpushbutton.h> +#include <qtimer.h> +#include <qpainter.h> +#include <qpixmap.h> + + +/* + Constructs a Hello widget. Starts a 40 ms animation timer. +*/ + +Hello::Hello( const char *text, TQWidget *parent, const char *name ) + : TQWidget(parent,name), t(text), b(0) +{ + TQTimer *timer = new TQTimer(this); + connect( timer, SIGNAL(timeout()), SLOT(animate()) ); + timer->start( 40 ); + + resize( 260, 130 ); +} + + +/* + This private slot is called each time the timer fires. +*/ + +void Hello::animate() +{ + b = (b + 1) & 15; + repaint( FALSE ); +} + + +/* + Handles mouse button release events for the Hello widget. + + We emit the clicked() signal when the mouse is released inside + the widget. +*/ + +void Hello::mouseReleaseEvent( TQMouseEvent *e ) +{ + if ( rect().contains( e->pos() ) ) + emit clicked(); +} + + +/* + Handles paint events for the Hello widget. + + Flicker-free update. The text is first drawn in the pixmap and the + pixmap is then blt'ed to the screen. +*/ + +void Hello::paintEvent( TQPaintEvent * ) +{ + static int sin_tbl[16] = { + 0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38}; + + if ( t.isEmpty() ) + return; + + // 1: Compute some sizes, positions etc. + TQFontMetrics fm = fontMetrics(); + int w = fm.width(t) + 20; + int h = fm.height() * 2; + int pmx = width()/2 - w/2; + int pmy = height()/2 - h/2; + + // 2: Create the pixmap and fill it with the widget's background + TQPixmap pm( w, h ); + pm.fill( this, pmx, pmy ); + + // 3: Paint the pixmap. Cool wave effect + TQPainter p; + int x = 10; + int y = h/2 + fm.descent(); + int i = 0; + p.begin( &pm ); + p.setFont( font() ); + while ( !t[i].isNull() ) { + int i16 = (b+i) & 15; + p.setPen( TQColor((15-i16)*16,255,255,TQColor::Hsv) ); + p.drawText( x, y-sin_tbl[i16]*h/800, t.mid(i,1), 1 ); + x += fm.width( t[i] ); + i++; + } + p.end(); + + // 4: Copy the pixmap to the Hello widget + bitBlt( this, pmx, pmy, &pm ); +} diff --git a/examples/hello/hello.doc b/examples/hello/hello.doc new file mode 100644 index 000000000..8c001a2b8 --- /dev/null +++ b/examples/hello/hello.doc @@ -0,0 +1,30 @@ + +/* +*/ +/*! \page hello-example.html + + \ingroup examples + \title Hello, World + + This example brings up the words "Hello, World" moving up and down, + and in different colors. + + <hr> + + Header file: + + \include hello/hello.h + + <hr> + + Implementation: + + \include hello/hello.cpp + + <hr> + + Main: + + \include hello/main.cpp +*/ + diff --git a/examples/hello/hello.h b/examples/hello/hello.h new file mode 100644 index 000000000..c519a3351 --- /dev/null +++ b/examples/hello/hello.h @@ -0,0 +1,33 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#ifndef HELLO_H +#define HELLO_H + +#include <qwidget.h> + + +class Hello : public TQWidget +{ + Q_OBJECT +public: + Hello( const char *text, TQWidget *parent=0, const char *name=0 ); +signals: + void clicked(); +protected: + void mouseReleaseEvent( TQMouseEvent * ); + void paintEvent( TQPaintEvent * ); +private slots: + void animate(); +private: + TQString t; + int b; +}; + +#endif diff --git a/examples/hello/hello.pro b/examples/hello/hello.pro new file mode 100644 index 000000000..de15e8ef2 --- /dev/null +++ b/examples/hello/hello.pro @@ -0,0 +1,9 @@ +TEMPLATE = app +TARGET = hello + +CONFIG += qt warn_on release +DEPENDPATH = ../../include + +HEADERS = hello.h +SOURCES = hello.cpp \ + main.cpp diff --git a/examples/hello/main.cpp b/examples/hello/main.cpp new file mode 100644 index 000000000..45e9e58e7 --- /dev/null +++ b/examples/hello/main.cpp @@ -0,0 +1,40 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "hello.h" +#include <qapplication.h> + + +/* + The program starts here. It parses the command line and builds a message + string to be displayed by the Hello widget. +*/ + +int main( int argc, char **argv ) +{ + TQApplication a(argc,argv); + TQString s; + for ( int i=1; i<argc; i++ ) { + s += argv[i]; + if ( i<argc-1 ) + s += " "; + } + if ( s.isEmpty() ) + s = "Hello, World"; + Hello h( s ); +#ifndef QT_NO_WIDGET_TOPEXTRA // for TQt/Embedded minimal build + h.setCaption( "TQt says hello" ); +#endif + TQObject::connect( &h, SIGNAL(clicked()), &a, SLOT(tquit()) ); + h.setFont( TQFont("times",32,TQFont::Bold) ); // default font + h.setBackgroundColor( TQt::white ); // default bg color + a.setMainWidget( &h ); + h.show(); + return a.exec(); +} |