Qt之对话框设计——电子时钟

来源:互联网 发布:mac炒股软件哪个好 编辑:程序博客网 时间:2024/04/30 04:37

digiclock.h

  1. #ifndef DIGICLOCK_H  
  2. #define DIGICLOCK_H  
  3.   
  4. #include <QLCDNumber>  
  5. #include <QPoint>  
  6.   
  7. class DigiClock : public QLCDNumber  
  8. {  
  9.     Q_OBJECT  
  10.   
  11. public:  
  12.     DigiClock(QWidget *parent = 0);  
  13.     ~DigiClock();  
  14.   
  15.     void mousePressEvent(QMouseEvent *);  
  16.     void mouseMoveEvent(QMouseEvent *);  
  17.   
  18. public slots:  
  19.     void showTime();  
  20.   
  21. private:  
  22.     QPoint  dragPosition;   //鼠标点击位置相对窗体左上角的偏移量  
  23.     bool showColon;     //是否显示“:”  
  24. };  
  25.   
  26. #endif // CLOCK_H  

digiclock.cpp

  1. #include "digiclock.h"  
  2. #include <QPalette>  
  3. #include <QTimer>  
  4. #include <QTime>  
  5. #include <QMouseEvent>  
  6.   
  7. DigiClock::DigiClock(QWidget *parent)  
  8.     : QLCDNumber(parent)  
  9. {  
  10.     QPalette plt;   //实例化调色板对象  
  11.     plt.setColor(QPalette::Window,Qt::blue);  
  12.     setPalette(plt);  
  13.   
  14.     setWindowFlags(Qt::FramelessWindowHint);  
  15.   
  16.     setWindowOpacity(0.5);      //设置不透明度  
  17.   
  18.     QTimer *timer = new QTimer(this);  
  19.     connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));  
  20.     timer->start(1000);  
  21.   
  22.     showTime();  
  23.   
  24.     resize(150,60);  
  25.     showColon = true;  
  26. }  
  27.   
  28. DigiClock::~DigiClock()  
  29. {  
  30.   
  31. }  
  32.   
  33. void DigiClock::showTime()  
  34. {  
  35.     QTime time = QTime::currentTime();  
  36.     QString text = time.toString("hh:mm");  
  37.     if(showColon)  
  38.     {  
  39.         text[2] = ':';  
  40.         showColon = false;  
  41.     }  
  42.     else  
  43.     {  
  44.         text[2] = ' ';  
  45.         showColon = true;  
  46.     }  
  47.     display(text);  
  48. }  
  49.   
  50. void DigiClock::mousePressEvent(QMouseEvent * e)  
  51. {  
  52.     if (e->button() == Qt::LeftButton)  
  53.     {  
  54.         dragPosition = e->globalPos() - frameGeometry().topLeft();  
  55.         e->accept();  
  56.     }  
  57.     if(e->button() == Qt::RightButton)  
  58.     {  
  59.         close();  
  60.     }  
  61. }  
  62.   
  63. void DigiClock::mouseMoveEvent(QMouseEvent * e)  
  64. {  
  65.     if(e->buttons() & Qt::LeftButton)  
  66.     {  
  67.         move(e->globalPos() - dragPosition);  
  68.         e->accept();  
  69.     }  
  70. }  
0 0
原创粉丝点击