QT4 用QLabel实时显示时间

最近在做一个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();
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值