📄 hello-main-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - hello/main.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Hello, World</h1><br clear="all"> This example brings up the words "Hello, World" moving up and down, and in different colors. <hr> Header file: <pre>/****************************************************************************** $Id: qt/examples/hello/hello.h 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef HELLO_H#define HELLO_H#include <<a href="qwidget-h.html">qwidget.h</a>>class Hello : public QWidget{ Q_OBJECTpublic: Hello( const char *text, QWidget *parent=0, const char *name=0 );signals: void clicked();protected: void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * ); void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );private slots: void animate();private: <a href="qstring.html">QString</a> t; int b;};#endif</pre> <hr> Implementation: <pre>/****************************************************************************** $Id: qt/examples/hello/hello.cpp 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "hello.h"#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>#include <<a href="qtimer-h.html">qtimer.h</a>>#include <<a href="qpainter-h.html">qpainter.h</a>>#include <<a href="qpixmap-h.html">qpixmap.h</a>>/* Constructs a Hello widget. Starts a 40 ms animation timer.*/Hello::Hello( const char *text, QWidget *parent, const char *name ) : <a href="qwidget.html">QWidget</a>(parent,name), t(text), b(0){ <a href="qtimer.html">QTimer</a> *timer = new <a href="qtimer.html">QTimer</a>(this); <a href="qobject.html#fbde73">connect</a>( timer, SIGNAL(timeout()), SLOT(<a href=#21>animate</a>()) ); timer-><a href="qtimer.html#ce33bc">start</a>( 40 ); <a href="qwidget.html#8fcbbe">resize</a>( 260, 130 );}/* This private slot is called each time the timer fires.*/void <a name="21"></a>Hello::animate(){ b = (b + 1) & 15; <a href="qwidget.html#7569b1">repaint</a>( FALSE );}/* Handles mouse button release events for the Hello widget. We emit the clicked() signal when the mouse is released inside the widget.*/void <a name="22"></a>Hello::mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ){ if ( rect().contains( e-><a href="qmouseevent.html#ac6f25">pos</a>() ) ) 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 <a name="23"></a>Hello::paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * ){ 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. <a href="qfontmetrics.html">QFontMetrics</a> fm = fontMetrics(); int w = fm.<a href="qfontmetrics.html#3b6f39">width</a>(t) + 20; int h = fm.<a href="qfontmetrics.html#341253">height</a>() * 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 <a href="qpixmap.html">QPixmap</a> pm( w, h ); pm.<a href="qpixmap.html#6910a0">fill</a>( this, pmx, pmy ); // 3: Paint the pixmap. Cool wave effect <a href="qpainter.html">QPainter</a> p; int x = 10; int y = h/2 + fm.<a href="qfontmetrics.html#200b74">descent</a>(); int i = 0; p.<a href="qpainter.html#02ed5d">begin</a>( &pm ); p.<a href="qpainter.html#998df2">setFont</a>( <a href="qwidget.html#167922">font</a>() ); while ( !t[i].isNull() ) { int i16 = (b+i) & 15; p.<a href="qpainter.html#0183e4">setPen</a>( <a href="qcolor.html">QColor</a>((15-i16)*16,255,255,QColor::Hsv) ); p.<a href="qpainter.html#0f088f">drawText</a>( x, y-sin_tbl[i16]*h/800, t.mid(i,1), 1 ); x += fm.<a href="qfontmetrics.html#3b6f39">width</a>( t[i] ); i++; } p.<a href="qpainter.html#365784">end</a>(); // 4: Copy the pixmap to the Hello widget <a href="qpaintdevice.html#35ae2e">bitBlt</a>( this, pmx, pmy, &pm );}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/hello/main.cpp 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "hello.h"#include <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>/* 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 ){ <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a(argc,argv); <a name="QString"></a><a href="qstring.html">QString</a> s; for ( int i=1; i<argc; i++ ) { s += argv[i]; if ( i<argc-1 ) s += " "; } if ( s.<a name="isEmpty"></a><a href="qstring.html#c62623">isEmpty</a>() ) s = "Hello, World"; Hello h( s ); h.<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>( "Qt says hello" ); <a name="QObject::connect"></a><a href="qobject.html#7f8e37">QObject::connect</a>( &h, SIGNAL(clicked()), &a, SLOT(quit()) ); h.<a name="setFont"></a><a href="qwidget.html#090d60">setFont</a>( <a name="QFont"></a><a href="qfont.html">QFont</a>("times",32,QFont::Bold) ); // default font h.<a name="setBackgroundColor"></a><a href="qwidget.html#c09181">setBackgroundColor</a>( Qt::white ); // default bg color a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( &h ); h.<a name="show"></a><a href="qwidget.html#200ee5">show</a>(); return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -