//DigitalClock.h
#pragma once
#include <QtGui>
#include <QLCDNumber>
class DigitalClock : public QLCDNumber
{
public:
DigitalClock(QWidget *parent=0);
protected:
void timerEvent(QTimerEvent *);
private:
bool isColon;
};
//DigitalClock.cpp
#include "DigitalClock.h"
#include <QDateTime>
DigitalClock::DigitalClock(QWidget *parent) : QLCDNumber(parent)
{
this->isColon = false;
this->setFrameStyle(QFrame::Panel | QFrame::Raised);
this->setNumDigits(11);
//QPalette pe;
//pe.setColor(QPalette::WindowText,Qt::blue);
//setPalette(pe);
startTimer(500);
setStyleSheet("color:#0000ff;");
//setStyleSheet("color:blue;");
}
void DigitalClock::timerEvent(QTimerEvent *e) {
isColon = !isColon;
QString timeString = QTime::currentTime().toString().left(5);
QDate date = QDate::currentDate();
QString dateString;
dateString.sprintf( " %2d-%2d", date.month(), date.day());
QString displayString = timeString;
if (!isColon) {
displayString[2] = ' ';
}
display(displayString);
}
//main.cpp
#include "DigitalClock.h"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
DigitalClock *digitalClock = new DigitalClock;
digitalClock->resize(300,80);
digitalClock->setWindowTitle("QTimer");
digitalClock->show();
return app.exec();
}