#ifndef DIALOG_H
#define DIALOG_H
#include
#include
#include
#include
class Dialog : public QLCDNumber
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
public slots:
void showTime();
private:
QPoint dragPosition; //保存鼠标左上角偏移量
bool showColon; //用来显示时间是否显示
};
#endif // DIALOG_H
#include "dialog.h"
#include
#include
Dialog::Dialog(QWidget *parent)
: QLCDNumber(parent)
{
QPalette p = palette();
p.setColor(QPalette::Window,Qt::magenta);
setPalette(p); //设置背景
setDigitCount(8);
setWindowFlags(Qt::FramelessWindowHint);//设置一个没有边框的窗体
//setWindowOpacity(0.5);
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
timer->start(1000);
showTime();
showColon = true;
resize(150,60);
}
Dialog::~Dialog()
{
}
void Dialog::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
dragPosition = event->globalPos() - frameGeometry().topLeft(); //鼠标相对于时钟窗体左上角的偏移位置
event->accept();
}
if(event->button() == Qt::RightButton)
{
close();
}
}
void Dialog::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() &Qt::LeftButton)
{
move(event->globalPos() -dragPosition);
event->accept();
}
}
void Dialog::showTime()
{
QTime time = QTime::currentTime();//获得当前时间
QString text = time.toString("hh:mm:ss");
if(showColon)
{
text[2]=':';
text[5]=':';
showColon=false;
}
else
{
text[2]=' ';
text[5]=' ';
showColon = true;
}
display(text);
}
#include "dialog.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
QTime QLCDNumber电子时钟

最新推荐文章于 2022-11-07 13:56:20 发布
