Qt学习——电子时钟 .

来源:互联网 发布:js中on和bind的区别 编辑:程序博客网 时间:2024/04/30 05:31
 

中间的冒号是一秒闪烁一次

新建一个继承自QLCDNumber的类

头文件:

view plaincopy to clipboardprint?
  1. #ifndef DIGICLOCK_H   
  2. #define DIGICLOCK_H   
  3.   
  4. #include <QLCDNumber>   
  5.   
  6. class DIgiClock : public QLCDNumber  
  7. {  
  8.     Q_OBJECT  
  9. public:  
  10.     DIgiClock(QWidget *parent = 0);  
  11.     void mousePressEvent(QMouseEvent *);  
  12.     void mouseMoveEvent(QMouseEvent *);  
  13. public slots:  
  14.     void ShowTime();  
  15. private:  
  16.     QPoint dragPosition;  
  17.     bool showColon;  
  18. };  
  19.   
  20. #endif // DIGICLOCK_H  

源文件:

view plaincopy to clipboardprint?
  1. #include "digiclock.h"   
  2. #include <QTime>   
  3. #include <QTimer>   
  4. #include <QMouseEvent>   
  5.   
  6. DIgiClock::DIgiClock(QWidget *parent) :  
  7.     QLCDNumber(parent)  
  8. {  
  9.     QPalette p=palette();  
  10.     p.setColor(QPalette::Window,Qt::blue);  
  11.     setPalette(p);  
  12.   
  13.     setWindowFlags(Qt::FramelessWindowHint);  
  14.     setWindowOpacity(0.5);  
  15.   
  16.     QTimer *timer=new QTimer(this);  
  17.     connect(timer,SIGNAL(timeout()),this,SLOT(ShowTime()));  
  18.     timer->start(500);  
  19.     ShowTime();  
  20.     resize(150,60);  
  21.     showColon=true;  
  22. }  
  23.   
  24. void DIgiClock::ShowTime()  
  25. {  
  26.     QTime time=QTime::currentTime();  
  27.     QString text=time.toString("hh:mm");  
  28.     if(showColon)  
  29.     {  
  30.         text[2]=':';  
  31.         showColon=false;  
  32.     }  
  33.     else  
  34.     {  
  35.         text[2]=' ';  
  36.         showColon=true;  
  37.     }  
  38.     display(text);  
  39. }  
  40.   
  41. void DIgiClock::mousePressEvent(QMouseEvent *event)  
  42. {  
  43.     if(event->button()==Qt::LeftButton)  
  44.     {  
  45.         dragPosition=event->globalPos()-frameGeometry().topLeft();  
  46.         event->accept();  
  47.     }  
  48.     else if(event->button()==Qt::RightButton)  
  49.     {  
  50.         close();  
  51.     }  
  52. }  
  53. void DIgiClock::mouseMoveEvent(QMouseEvent *event)  
  54. {  
  55.     move(event->globalPos()-dragPosition);  
  56.     event->accept();  
  57. }  
原创粉丝点击