Qt中使用QLabel显示时间的两种方法

其实这两种方法思路一致,只是实现方法不一样而已。

main.cpp

[cpp]  view plain copy
  1. #include "displaytime.h"  
  2. #include <QApplication>  
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     QApplication a(argc, argv);  
  7.     DisplayTime w;  
  8.     w.show();  
  9.       
  10.     return a.exec();  
  11. }  
[cpp]  view plain copy
  1.   
[cpp]  view plain copy
  1.   
[cpp]  view plain copy
  1.   

方法一:

displaytime.h

[cpp]  view plain copy
  1. #ifndef DISPLAYTIME_H  
  2. #define DISPLAYTIME_H  
  3.   
  4. #include <QWidget>  
  5. #include <QtGui>  
  6.   
  7. class QLabel;  
  8.   
  9. class DisplayTime : public QWidget  
  10. {  
  11.     Q_OBJECT  
  12.       
  13. public:  
  14.     DisplayTime(QWidget *parent = 0);  
  15.     ~DisplayTime();  
  16.   
  17. private:  
  18.     QLabel *timeLabel;  
  19.   
  20. protected:  
  21.     void timerEvent(QTimerEvent * event);  
  22. };  
  23.   
  24. #endif // DISPLAYTIME_H  


displaytime.cpp

[cpp]  view plain copy
  1. #include "displaytime.h"  
  2.   
  3. DisplayTime::DisplayTime(QWidget *parent)  
  4.     : QWidget(parent)  
  5. {  
  6.     timeLabel = new QLabel(this);  
  7.     timerEvent(0);  
  8.     startTimer(1000);  
  9.     timeLabel->show();  
  10. }  
  11.   
  12. DisplayTime::~DisplayTime()  
  13. {  
  14.       
  15. }  
  16.   
  17. void DisplayTime::timerEvent(QTimerEvent *event)  
  18. {  
  19.     Q_UNUSED(event);  
  20.     timeLabel->setText(QTime::currentTime().toString("hh:mm:ss"));  
  21. }  


方法二:


displaytime.h

[cpp]  view plain copy
  1. #ifndef DISPLAYTIME_H  
  2. #define DISPLAYTIME_H  
  3.   
  4. #include <QWidget>  
  5. #include <QtGui>  
  6.   
  7. class QLabel;  
  8.   
  9. class DisplayTime : public QWidget  
  10. {  
  11.     Q_OBJECT  
  12.       
  13. public:  
  14.     DisplayTime(QWidget *parent = 0);  
  15.     ~DisplayTime();  
  16.   
  17. private:  
  18.     QLabel *timeLabel;  
  19.   
  20. private slots:  
  21.     void updateTime();  
  22. };  
  23.   
  24. #endif // DISPLAYTIME_H<strong>  
  25. </strong>  


displaytime.cpp

[cpp]  view plain copy
  1. #include "displaytime.h"  
  2.   
  3. DisplayTime::DisplayTime(QWidget *parent)  
  4.     : QWidget(parent)  
  5. {  
  6.     timeLabel = new QLabel(this);  
  7.     timeLabel->setGeometry(0, 0, 150, 30);  
  8.   
  9.     QTimer *timer = new QTimer(this);  
  10.     connect(timer, SIGNAL(timeout()),  
  11.             this, SLOT(updateTime()));  
  12.     timer->start(1000);  
  13.   
  14.     timeLabel->show();  
  15. }  
  16.   
  17. DisplayTime::~DisplayTime()  
  18. {  
  19.       
  20. }  
  21.   
  22. void DisplayTime::updateTime()  
  23. {  
  24.     timeLabel->setText(QDateTime::currentDateTime().toString("hh:mm:ss"));  
  25. }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值