电子时钟QTimeQTimerQLCDNumber
Posted Respect@
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了电子时钟QTimeQTimerQLCDNumber相关的知识,希望对你有一定的参考价值。
电子时钟QTime、QTimer、QLCDNumber
电子时钟QTime、QTimer、QLCDNumber
clock.h
#ifndef CLOCK_H
#define CLOCK_H
#include <QDialog>
class Clock : public QDialog
{
Q_OBJECT
public:
Clock(QWidget *parent = 0);
~Clock();
};
#endif // CLOCK_H
digitalclock.h
#ifndef DIGITALCLOCK_H
#define DIGITALCLOCK_H
#include <QLCDNumber>
class DigitalClock : public QLCDNumber
{
Q_OBJECT
public:
DigitalClock(QWidget* parent = 0);
protected:
void mousePressEvent(QMouseEvent* event);//鼠标按下事件
void mouseMoveEvent(QMouseEvent* event); //鼠标移动事件
private slots:
void showTime(); //显示当前时间
private:
QPoint m_dragPosition; //保存鼠标点相对电子钟表窗体左上角的偏移值
bool m_showColon; //钟表显示“ :”
};
#endif // DIGITALCLOCK_H
clock.cpp
#include "clock.h"
Clock::Clock(QWidget *parent)
: QDialog(parent)
{
}
Clock::~Clock()
{
}
digitalclock.cpp
#include "digitalclock.h"
#include <QTimer>
#include <QTime>
#include <QMouseEvent>
DigitalClock::DigitalClock(QWidget* parent):QLCDNumber(parent)
{
//设置蓝色背景
QPalette p = palette();
p.setColor(QPalette::Window,Qt::blue);
setPalette(p);
//无边框窗体风格
setWindowFlags(Qt::FramelessWindowHint);
//窗体透明度
setWindowOpacity(0.5);
QTimer *timer = new QTimer(this);
connect(timer,&QTimer::timeout,this,&DigitalClock::showTime);
timer->start(1000);//周期为1000毫秒(1秒)
showTime();
resize(150,60);
m_showColon = true;
}
void DigitalClock::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
m_dragPosition = event->globalPos() //鼠标相对屏幕左上角的位置
- frameGeometry().topLeft();//时钟窗体左上角位置
event->accept();
}
if(event->button() == Qt::RightButton)
{
close();
}
}
void DigitalClock::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
move(event->globalPos() - m_dragPosition);
event->accept();
}
}
void DigitalClock::showTime()
{
QTime time = QTime::currentTime();
QString strTime = time.toString("hh:mm");
if(m_showColon){
strTime[2] = ':';
}else{
strTime[2] = ' ';
}
display(strTime);
m_showColon = !m_showColon;
}
main.cpp
#include "clock.h"
#include <QApplication>
#include "digitalclock.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DigitalClock w;
w.show();
return a.exec();
}
以上是关于电子时钟QTimeQTimerQLCDNumber的主要内容,如果未能解决你的问题,请参考以下文章