最近在做一个2410基于Linux的界面,需要实时的现实一些数据和时间在网上找了好多资料,费了九牛二虎之力再到一篇相关的文章,能显示时间,但却不能实时刷新。费了将近两天的时间,终于可以让它实时显示了。
编译环境 :Fedora 9
编译器 :qtopia-core-opensource-src-4.3.3.tar.gz(在Fedora 9上编译成功也费了好长的时间)
功能已实现,程序如下,供大家参考,
由于本人也是QT的初学者,很希望高手能指点指点
// ClockWindow.h
#ifndef _CLOCKWINDOW_H
#define _CLOCKWINDOW_H
#include <QApplication>
#include <QLabel>
class ClockWindow : public QLabel
{
Q_OBJECT
public:
ClockWindow(QWidget *parent = 0);
protected:
void timerEvent(QTimerEvent * event);
};
#endif
// ClockWindow.cpp
#include "clockwindow.h"
#include <QDateTime>
ClockWindow::ClockWindow(QWidget *parent)
: QLabel(parent)
{
setMargin(10);
setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
timerEvent(0);
startTimer(1000);
}
void ClockWindow::timerEvent(QTimerEvent *event)
{
QDateTime qdatetime=QDateTime::currentDateTime();
int y=qdatetime.date().year();
int m=qdatetime.date().month();
int d=qdatetime.date().day();
QString strTime=qdatetime.time().toString();
QLabel *TextLabel_Time = new QLabel(this);
TextLabel_Time->setText(strTime+" "+QString::number(d)+"/"+QString::number(m)+"/"+QString::number(y)); //显示时间格式为21:09:54 9/4/2009
QTimer *time = new QTime(TextLabel_Time);
QTimer *timec = new QTime(TextLabel_Time)
connect(time,SIGNAL(timeout()),TextLabel_Time,SLoT(show()));
connect(timec,SIGNAL(timeout()),TextLabel_Time,SLoT(clear()));
time->start(1000);
timec->start(2000);
// main.cpp
#include "clockwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ClockWindow window;
window.show();
return app.exec();
}