ledclock
ledclock总共包含三个文件:digiclock.h,digiclock.cpp,main.cpp
三个文件的代码分别如下:
digiclock.h:
#ifndef DIGICLOCK_H
#define DIGICLOCK_H
#include
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.cpp:
#include "digiclock.h"
DigiClock::DigiClock(QWidget *parent)
: QLCDNumber(parent)
{
QPalette p = palette();
p.setColor(QPalette::Window,Qt::red);
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(100, 40);
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 * e)
{
if (e->button() == Qt::LeftButton)
{
dragPosition = e->globalPos() - frameGeometry().topLeft();
e->accept();
}
if (e->button() == Qt::RightButton)
{
close();
}
}
void DigiClock::mouseMoveEvent(QMouseEvent * e)
{
if (e->buttons() & Qt::LeftButton)
{
move(e->globalPos() - dragPosition);
e->accept();
}
}
main.cpp:
#include
#include "digiclock.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
DigiClock clock;
clock.show();
return app.exec();
}
效果如下:
|
文件: |
digiclock.rar |
大小: |
35KB |
下载: |
下载 | |
阅读(1203) | 评论(2) | 转发(0) |