中间的冒号是一秒闪烁一次
新建一个继承自QLCDNumber的类
头文件:
- #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
源文件:
- #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();
- }