#include <qapplication.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <qlabel.h>
#include <time.h>
#include <qtimer.h>
class Clock:public QWidget
{
Q_OBJECT;
public:
Clock(QWidget *parent = NULL, const char *name = NULL);
public slots:
void TimeChange();
private:
QPushButton *exit;
QLabel *labSee;
struct tm *ti;
char timeStr[20];
};
Clock::Clock(QWidget *parent, const char *name):QWidget(parent, name)
{
setGeometry(100, 100, 200, 120);
setMinimumSize(200,120);
setMaximumSize(400,240);
labSee = new QLabel(this);
exit = new QPushButton("exit",this);
exit->setFont(QFont("Times",18,QFont::Bold));
connect(exit,SIGNAL(clicked()),qApp,SLOT(quit()));
TimeChange();
QTimer *timer = new QTimer;
timer->changeInterval(1000);
connect(timer,SIGNAL(timerout()),this,SLOT(TimeChange()));
timer->start(1000);
}
void Clock::TimeChange()
{
time_t t = time(NULL);
ti = localtime(&t);
sprintf(timeStr,"Time:%-2d,%-2d,%-2d",ti->tm.hour,ti->tm.min,ti->tm.sec);
labSee->setText(timeStr);
labSee->setAlignment(AlignCenter);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Clock clock_c;
clock_c.show();
return a.exec();
}
[ 此贴被style在2006-05-15 19:03重新编辑 ]