QT----电子时钟

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  
#ifndef DIGICLOCK_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"   
  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. }  
#include "digiclock.h"
#include <QTime>
#include <QTimer>
#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(500);
    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();
    }
    else if(event->button()==Qt::RightButton)
    {
        close();
    }
}
void DIgiClock::mouseMoveEvent(QMouseEvent *event)
{
    move(event->globalPos()-dragPosition);
    event->accept();
}

上述代码主要有三点:

1,显示时间:

[cpp]
QTimer *timer=new QTimer(this);  
  1. connect(timer,SIGNAL(timeout()),this,SLOT(ShowTime()));  
  2. timer->start(500);  
    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(ShowTime()));
    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. }  
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);
}
获取当前时间,然后放到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. }  
void DIgiClock::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        dragPosition=event->globalPos()-frameGeometry().topLeft();
        event->accept();
    }
    else if(event->button()==Qt::RightButton)
    {
        close();
    }
}

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

3,响应鼠标拖动

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

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

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

拖动的位置减去上一次位置就计算出来左上角应该移动的位置差。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值