27.Qt时钟

  • myclock.h
     1 #ifndef MYCLOCK_H
     2 #define MYCLOCK_H
     3 
     4 #include <QObject>
     5 #include <QLCDNumber>
     6 #include <QWidget>
     7 #include <QMouseEvent>
     8 
     9 class myclock : public QLCDNumber
    10 {
    11     Q_OBJECT
    12 public:
    13     myclock(QWidget* parent=0);
    14     void mousePressEvent(QMouseEvent *event);
    15     void mouseMoveEvent(QMouseEvent *event);
    16 private:
    17     QPoint pos;
    18     bool showcolon;
    19 
    20 private slots:
    21     void showtime();
    22 };
    23 
    24 #endif // MYCLOCK_H

     

  • myclock.cpp 1 #include "myclock.h"
     2 #include <QTimer>
     3 #include <QTime>
     4 #include <QPalette>
     5 
     6 myclock::myclock(QWidget* parent):QLCDNumber(parent)
     7 {
     8     //设定颜色
     9     QPalette p = palette();
    10     p.setColor(QPalette::Window,Qt::blue);
    11     setPalette(p);
    12 
    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 showtime(); 22 resize(300,120); 23 } 24 25 void myclock::mousePressEvent(QMouseEvent *event) 26 { 27 28 } 29 30 void myclock::mouseMoveEvent(QMouseEvent *event) 31 { 32 33 } 34 35 void myclock::showtime() 36 { 37 QTime time = QTime::currentTime();//当前时间 38 QString str = time.toString("hh:mm:ss");//时间 39 this->display(str); 40 }

     

posted @ 2018-04-09 22:23  喵小喵~  阅读(216)  评论(0编辑  收藏  举报