LEDClock时钟

来源:互联网 发布:澳洲goat soap知乎 编辑:程序博客网 时间:2024/05/01 06:17

头文件.h

 

#ifndefDIGICLOCK_H

#define DIGICLOCK_H
#include <QLCDNumber>
class DigiClock : public QLCDNumber
{
    Q_OBJECT
public:
    DigiClock(QWidget *parent=0);
    void mousePressEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
public slots:
    void showTime();                 //显示当前的时间
private:
    QPoint dragPosition;            //保存鼠标点相对电子时钟窗体左上角的偏移值
    bool showColon;                  //用于显示时间时是否显示“:”
};
#endif // DIGICLOCK_H
 
.cpp文件
 
#include "digiclock.h"
#include <QTimer>
#include <QTime>
#include <QMouseEvent>
DigiClock::DigiClock(QWidget *parent):QLCDNumber(parent)
{
    QPalette p=palette();   //调色板
    p.setColor(QPalette::Window,Qt::blue);
    setPalette(p);
    setWindowFlags(Qt::FramelessWindowHint);
    setWindowOpacity(0.5);//设置透明度
    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
    timer->start(1000);
    showTime();
    resize(150,60);
    showColon=true;//初始化
}
void DigiClock::showTime()
{
    QTime time=QTime::currentTime();
    QString text=time.toString("hh:mm");
    if(showColon)
    {
        text[2]=':';
        showColon=false;
    }
    else
    {
        text[2]=' ';
        showColon=true;
    }
    display(text);
}
void DigiClock::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        dragPosition=event->globalPos()-frameGeometry().topLeft();
        event->accept();//事件已处理,不会向下传递
    }
    if(event->button()==Qt::RightButton)
    {
        close();
    }
}
void DigiClock::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()&Qt::LeftButton)
    {
        move(event->globalPos()-dragPosition);
        event->accept();//事件已处理,不再向下传递
    }
}
 
 
0 0