您的位置:首页 > 编程语言 > Qt开发

QTime QLCDNumber电子时钟

2016-12-02 21:49 309 查看


#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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: