不懂的时候看看别人写的文章,然后发现写的真棒,就转载了,很少自己去写,主要写的太差了,语言组织能力实在烂的可以,怪不得语文很少及格,诶。。。。
要是写的不好,求大神指出,谢谢
qtimer 很像单片机里面的定时器,设定时间间隔,超时了就会发出一个信号(timerout()),通知你时间到了。这时候便会自动执行你自己定义槽函数,
当然首先你要连接相应的信号与槽,所以最简单定时器应用就应该是这样
QTimer *timer = new QTimer( myObject );
connect( timer, SIGNAL(
timeout()), this, SLOT(timerDone()) ); timer->
start( 2000, TRUE ); // 2秒单触发定时器这里的TRUE表示只触发一次,事实上还有一个静态函数可以达到这样的单处罚效果
singleShot(),
使用方法如下: #include <
qapplication.h> #include <
qtimer.h> int main( int argc, char **argv ) {
QApplication a( argc, argv ); QTimer::
singleShot( 10*60*1000, &a, SLOT(
quit()) );//quit为退出线程 ... // 创建并且显示你的窗口部件 return a.
exec(); }
如果想循环出发就只需要设置为FALSE即可。
如果想要相对复杂一点的定时器,每一次的定时时间不一样,qtimer类也提供了一个函数changeInterval (msec),单位为毫秒放在定时器的槽函数中
changeInterval (1000);下次定时时间为一秒
那如果不像要这个定时器了,则停止定时器
stop();
最后提供一个例子,来源于官方,
Hello, WorldThis example brings up the words "Hello, World" moving up and down, and in different colors.
Header file:
/****************************************************************************** $Id: qt/hello.h 3.0.5 edited Oct 12 2001 $**** 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 <
qwidget.h>class Hello : public
QWidget{
Q_OBJECTpublic: Hello( const char *text, QWidget *parent=0, const char *name=0 );signals: void clicked();protected: void mouseReleaseEvent(
QMouseEvent * ); void paintEvent(
QPaintEvent * );private slots: void animate();private:
QString t; int b;};#endif
Implementation:
/****************************************************************************** $Id: qt/hello.cpp 3.0.5 edited May 7 17:30 $**** 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 <
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, QWidget *parent, const char *name ) :
QWidget(parent,name), t(text), b(0){
QTimer *timer = new
QTimer(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(
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() ) return; // 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() ) { int i16 = (b+i) & 15; p.setPen( QColor((15-i16)*16,255,255,QColor::Hsv) ); p.drawText( x, y-sin_tbl[i16]*h/800, t.mid(i,1), 1 ); x += fm.width( t ); i++; } p.end(); // 4: Copy the pixmap to the Hello widget bitBlt( this, pmx, pmy, &pm );}
Main:
/****************************************************************************** $Id: qt/main.cpp 3.0.5 edited Oct 12 2001 $**** 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 <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 ){ QApplication a(argc,argv); QString s; for ( int i=1; i; if ( iisEmpty() ) s = "Hello, World"; Hello h( s );#ifndef QT_NO_WIDGET_TOPEXTRA // for Qt/Embedded minimal build h.setCaption( "Qt says hello" );#endif QObject::connect( &h, SIGNAL(clicked()), &a, SLOT(quit()) ); h.setFont( QFont("times",32,QFont::Bold) ); // default font h.setBackgroundColor( Qt::white ); // default bg color a.setMainWidget( &h ); h.show(); return a.exec();}