设置一个闹钟能实时显示当前时间
使用说明:按下 “设置”按键 进入设置模式,“开始”按键按一次,时加1,“停止”按键按一次,分加一,设置好后再次按下“设置”按键,闹钟设置完成并弹窗提示,设置完成后按下“启动“按键”,开始启动闹钟,到点自动响铃同时“启动”按键失效(无法按下),按下“停止”,停止响铃,“启动”按键生效,允许响铃。
.h文件
#ifndef MYVCOLOCK_H
#define MYVCOLOCK_H
#include <QWidget>
#include <QLCDNumber>
#include <QPushButton>
#include <QTextToSpeech>
#include <QTextEdit>
#include <QMessageBox>
class MyVColock : public QWidget
{
Q_OBJECT
signals:
void signal_1();//闹钟设置完成发射一个完成信号
public Q_SLOTS:
void on_setting_btn_clicked();//设置闹钟
void on_start_btn_clicked();//启用闹钟
void on_stop_btn_clicked();//关闭闹钟
public:
MyVColock(QWidget *parent = nullptr);
~MyVColock();
void timerEvent(QTimerEvent *);//定时事件
void mousePressEvent(QMouseEvent *e);//鼠标按下事件
void mouseMoveEvent(QMouseEvent *e);//鼠标移动事件
private:
QLCDNumber* text_LCD;//显示时间
QTextEdit* text_edit;//备忘录
QPushButton* setting_btn;//设置闹钟按钮
QPushButton* start_btn;//启用闹钟按钮
QPushButton* stop_btn;//关闭闹钟按钮
QTextToSpeech speech;//播报者
bool showflag;//秒 :是否显示
QPoint movePoint;//起始位置
QString set_time;//设置闹钟事件
int timer_id;//定时器编号
int hour = 0, min = 0;//闹钟时间 时、分
int time_setting = 0; //启用设置时间
bool say_flag;//是否播报
};
#endif // MYVCOLOCK_H
.cpp文件
#include "myvcolock.h"
#include<QTime>
#include<QMouseEvent>
void MyVColock::on_setting_btn_clicked()
{
if((++time_setting)%2 == 1)//奇数次按下设置闹钟时间,偶数次按下,设置完成
{
killTimer(timer_id);//关闭定时
char time[6];//中间变量
sprintf(time, "%d:%d",hour, min);//拼接字符串
QString set_time(time);//char*转换成QString类型
text_LCD->display(time);//显示设置闹钟时间时间
}else
{
emit signal_1();//设置完成,发射信号
timer_id = startTimer(1000);//启用定时
}
}
void MyVColock::on_start_btn_clicked()
{
if(time_setting%2 == 1)//奇数时设置时间,偶数次不设置
{
hour = (hour+1)%24;//时加1
//显示时间
char time[6];
sprintf(time, "%d:%d",hour, min);
QString set_time(time);
text_LCD->display(set_time);
}else
{
say_flag = true;//允许播报
start_btn->setEnabled(false);//启动定时后此按键不可用
}
}
void MyVColock::on_stop_btn_clicked()
{
if(time_setting%2 == 1)//奇数时设置时间,偶数次不设置
{
min = (min+1)%60;//分加1
//显示时间
char time[6];
sprintf(time, "%d:%d",hour, min);
QString set_time(time);
text_LCD->display(set_time);
}else
{
say_flag = false;//禁止播报
start_btn->setEnabled(true);//禁止播报后允许重新使用启动
}
}
MyVColock::MyVColock(QWidget *parent)
: QWidget(parent)
{
this->resize(300,100);
this->setWindowFlag(Qt::FramelessWindowHint);//去掉标题
QPalette p = this->palette();//背景颜色
p.setColor(QPalette::Window, Qt::green);//设置颜色
this->setPalette(p);
this->setWindowOpacity(0.8);//透明度
timer_id = startTimer(1000);//启用定时,1秒发一次信号
//时间显示属性设置
text_LCD = new QLCDNumber(this);
text_LCD->resize(210,70);//大小
//备忘录属性设置
text_edit = new QTextEdit(this);
text_edit->resize(300,50);//大小
text_edit->move(0,70);//位置
//设置按钮属性设置
setting_btn = new QPushButton(this);
setting_btn->setText("设置");//名字
setting_btn->resize(90,30);//大小
setting_btn->move(width()-90,5);//位置
//启动按钮属性设置
start_btn = new QPushButton(this);
start_btn->setText("启动");
start_btn->resize(45,30);
start_btn->move(width()-90,40);
//停止按钮属性设置
stop_btn = new QPushButton(this);
stop_btn->setText("停止");
stop_btn->resize(45,30);
stop_btn->move(width()-45,40);
//连接到对应槽函数
connect(setting_btn, &QPushButton::clicked, this, &MyVColock::on_setting_btn_clicked);
connect(start_btn, &QPushButton::clicked, this, &MyVColock::on_start_btn_clicked);
connect(stop_btn, &QPushButton::clicked, this, &MyVColock::on_stop_btn_clicked);
//设置完成弹窗提示
connect(this, &MyVColock::signal_1, [&](){
QMessageBox::information(this, "提示","设置完成",QMessageBox::Yes); });
}
MyVColock::~MyVColock()
{
}
//定时事件
void MyVColock::timerEvent(QTimerEvent *)
{
QTime systime = QTime::currentTime();//获取当前时间
QString timeText = systime.toString("hh:mm");//转换成QString类型
char time[6];
sprintf(time, "%02d:%02d",hour, min);//响铃时间
QString set_time(time);//char*转换成QString类型
if(timeText == set_time)//当前时间与响铃时间一致
{
if(say_flag)//是否允许播报
{
speech.say(text_edit->toPlainText());//开始播报内容
}
}
//显示时间(是否显示秒' :')
if(showflag)
{
timeText[2] = ':';
showflag = false;
}else
{
timeText[2] = ' ';
showflag = true;
}
text_LCD->display(timeText);
}
//重写鼠标按下事件
void MyVColock::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::LeftButton)//左按键按下可拖拽
{
movePoint = e->globalPos() - this->frameGeometry().topLeft();
}
if(e->button() == Qt::RightButton)//右键按下可关闭进程
{
this->close();
}
}
//重写鼠标移动事件
void MyVColock::mouseMoveEvent(QMouseEvent *e)
{
if(e->buttons() == Qt::LeftButton)
{
this->move(e->globalPos() - movePoint);
}
}
.测试结果: