QT----电子时钟

来源:互联网 发布:知乎 低价服务器 编辑:程序博客网 时间:2024/09/21 08:57
1614人阅读 评论(0)收藏 举报

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

新建一个继承自QLCDNumber的类

头文件:

[cpp]
#ifndef DIGICLOCK_H   
  1. #define DIGICLOCK_H   
  2.   
  3. #include <QLCDNumber>   
  4.   
  5. class DIgiClock : public QLCDNumber  
  6. {  
  7.     Q_OBJECT  
  8. public:  
  9.     DIgiClock(QWidget *parent = 0);  
  10.     void mousePressEvent(QMouseEvent *);//重载函数响应鼠标按下  
  11.     void mouseMoveEvent(QMouseEvent *);//重载函数响应鼠标移动  
  12. public slots:  
  13.     void ShowTime();  
  14. private:  
  15.     QPoint dragPosition;  
  16.     bool showColon;  
  17. };  
  18.   
  19. #endif // DIGICLOCK_H  

源文件:

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

上述代码主要有三点:

1,显示时间:

[cpp]
QTimer *timer=new QTimer(this);  
  1. connect(timer,SIGNAL(timeout()),this,SLOT(ShowTime()));  
  2. timer->start(500);  
每隔500毫秒调用一下ShowTime()槽函数。

[cpp]
void DIgiClock::ShowTime()  
  1. {  
  2.     QTime time=QTime::currentTime();  
  3.     QString text=time.toString("hh:mm");  
  4.     if(showColon)  
  5.     {  
  6.         text[2]=':';  
  7.         showColon=false;  
  8.     }  
  9.     else  
  10.     {  
  11.         text[2]=' ';  
  12.         showColon=true;  
  13.     }  
  14.     display(text);  
  15. }  
获取当前时间,然后放到QString中,然后调用父类方法display出来,这里的QString就有限制了,只能是QLCDNumber能显示的内容才行。

其中中间冒号间歇出现。

2,响应鼠标按下

[cpp]
  1. void DIgiClock::mousePressEvent(QMouseEvent *event)  
  2. {  
  3.     if(event->button()==Qt::LeftButton)  
  4.     {  
  5.         dragPosition=event->globalPos()-frameGeometry().topLeft();  
  6.         event->accept();  
  7.     }  
  8.     else if(event->button()==Qt::RightButton)  
  9.     {  
  10.         close();  
  11.     }  
  12. }  

左键记录按下的位置,右键关闭

3,响应鼠标拖动

[cpp]
void DIgiClock::mouseMoveEvent(QMouseEvent *event)  
  1. {  
  2.     move(event->globalPos()-dragPosition);  
  3.     event->accept();  
  4. }  
利用move函数将程序移动到指定位置,由于是窗口左上角移动到什么地方,所以这里做了计算。

计算内容应该是拖动的位置减去上一次位置的差加上左上角位置:

[cpp]
frameGeometry().topLeft()+【event->globalPos(当前位置)-event->globalPos(按下时候)】  

拖动的位置减去上一次位置就计算出来左上角应该移动的位置差。