/* 以下是手动添加的代码 */
t = "Hello,World";
b = 0;
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(animate()));
timer->start(40);
}
/*
* Destroys the object and frees any allocated resources
*/
Hello::~Hello()
{
}
/* 以下至结尾是手动添加的代码 */
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(QMouseEvent *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(QPaintEvent *)
{
static int sin_tbl[16] = {0, 38, 71, 92, 100, 92,
71, 38, 0, -38, -71, -92, -100, -92, -71, -38};
if (t.isEmpty())
eturn;
/* 1: Compute some sizes, positions etc. */
QFontMetrics 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 */
QPixmap pm(w, h);
pm.fill(this, pmx, pmy);
/* 3: Paint the pixmap. Cool wave effect */
QPainter p;
int x = 10;
int y = h/2 + fm.descent();
int i = 0;
p.begin(&pm);
p.setFont(font());
while (!t.isNull())
{
nt i16 = (b+i) & 15;
.setPen(QColor((15-i16)*16,255,255,QColor::Hsv));
wText(x, y-sin_tbl[i16]*h/800, t.mid(i,1), 1);
+= fm.width(t);
+;
}
p.end();
/* 4: Copy the pixmap to the Hello widget */
bitBlt(this, pmx, pmy, &pm);
}
/****************************************************************************
** 以下是 main.cpp 源代码
****************************************************************************/
#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.
*/
#define QT_NO_WIZARD
int main(int argc, char **argv)
{
QApplication a(argc,argv);
Hello dlg;
QObject::connect(&dlg, SIGNAL(clicked()), &a, SLOT(quit()));
a.setMainWidget(&dlg);
dlg.show();
return a.exec();
}
(5)编辑工程文件hello.pro文件。
到目前为止,为Hello,World例子编写了一个头文件和两个源文件,这3个文件应该被包括在工程文件中,因此还需要编辑hello.pro文件,加入hello.h、hello.cpp、main.cpp这3个文件名。具体定义如下: