Implementation of Qt-LCD electronic clock and qt-LCD electronic clock
First.
In this way, a simple time is displayed.
Note that we 'd better create an empty file. here we need to create a class that integrates QLCDNumber.
The specific method is as follows:
Source code
Digiclock. h
#ifndef DIGICLOCK_H#define DIGICLOCK_H#include
#include
class DigiClock : public QLCDNumber{ Q_OBJECTpublic: DigiClock(QWidget *parent = 0); void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event);private slots: void showTime();private: QPoint dragPosition; bool showColon;};#endif // DIGICLOCK_H
Digiclock. cpp
#include "digiclock.h"#include
#include
#include
#include
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::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(); }}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);}