头文件 digiclock.h
#ifndefDIGICLOCK_H
#defineDIGICLOCK_H
#include<QLCDNumber>
classDIgiClock:publicQLCDNumber
{
Q_OBJECT
public:
DIgiClock(QWidget*parent=0);
//voidmousePressEvent(QMouseEvent*);//重载函数响应鼠标按下
//voidmouseMoveEvent(QMouseEvent*);//重载函数响应鼠标移动
publicslots:
voidShowTime();
private:
QPointdragPosition;
boolshowColon;
};
#endif//DIGICLOCK_H
digiclock.cpp
#include"digiclock.h"
#include<QTime>
#include<QTimer>
#include<QMouseEvent>
DIgiClock::DIgiClock(QWidget*parent):
QLCDNumber(parent)
{
QPalettep=palette();
p.setColor(QPalette::Window,Qt::blue);
setPalette(p);
setWindowFlags(Qt::WindowMinMaxButtonsHint);
setWindowOpacity(0.5);
QTimer*timer=newQTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(ShowTime()));
timer->start(500);
ShowTime();
resize(200,110);
showColon=true;
}
voidDIgiClock::ShowTime()
{
QTimetime=QTime::currentTime();
QStringtext=time.toString("hh:mm:ss");
if(showColon)
{
text[2]=':';
text[5]=':';
showColon=false;
}
else
{
text[2]='';
text[5]='';
showColon=true;
}
display(text);
}
主函数
#include<QtGui/QApplication>
#include"digiclock.h"
#include<QTextCodec>
intmain(intargc,char*argv[])
{
QApplicationa(argc,argv);
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
DIgiClockw;
w.show();
returna.exec();
}