20230407 定时闹钟

该代码实现了一个基于Qt的定时闹钟应用,使用QTime进行时间比较,QTextToSpeech进行语音播报,QTimer触发时间检查,用户可以设置小时、分钟和秒来设定闹钟,当当前时间与设定时间匹配时,程序会播放提醒并显示消息框。
摘要由CSDN通过智能技术生成

定时闹钟

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDebug>
#include <QIntValidator>
#include <QTime>
#include <QMessageBox>
#include <QTextToSpeech>
#include <QTimer>
#include <QMouseEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public slots:
    void checkTime();

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    QTime *qTime;
    QTextToSpeech *speaker;
    QTimer *timer;
    QPoint startPoint;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
private slots:
    void on_pushButton1_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

#include "widget.h"
#include "ui_widget.h"

void Widget::checkTime()
{
    QTime curr = QTime::currentTime();
    if(curr.toString("hh:mm:ss") == this->qTime->toString("hh:mm:ss")){
        QString str = ui->textEdit1->toPlainText();
        if(str == ""){
            str = "时间已到";
        }
        speaker->say(str);
        QMessageBox::information(this,"提示","时间已到");
        this->close();
    }
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(QSize(800,500));
    this->setWindowFlag(Qt::FramelessWindowHint);

    this->speaker = new QTextToSpeech(this);
    //闹钟的时 分 秒设置校验器
    QIntValidator *validator1 = new QIntValidator(0,23,ui->lineEdit1);
    QIntValidator *validator2 = new QIntValidator(0,59,ui->lineEdit2);
    QIntValidator *validator3 = new QIntValidator(0,59,ui->lineEdit3);
    ui->lineEdit1->setValidator(validator1);
    ui->lineEdit2->setValidator(validator2);
    ui->lineEdit3->setValidator(validator3);
    ui->lineEdit1->setInputMethodHints(Qt::ImhDigitsOnly);
    ui->lineEdit2->setInputMethodHints(Qt::ImhDigitsOnly);
    ui->lineEdit3->setInputMethodHints(Qt::ImhDigitsOnly);

    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, QOverload<>::of(&Widget::checkTime));

}

Widget::~Widget()
{
    delete ui;
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    startPoint = event->globalPos() - this->frameGeometry().topLeft();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos() - startPoint );
}


void Widget::on_pushButton1_clicked()
{
    int hh = -1,mm=-1,ss=-1;
    if(ui->lineEdit1->text()!=""){
        hh = ui->lineEdit1->text().toInt();
    }
    if(ui->lineEdit2->text()!=""){
        mm = ui->lineEdit2->text().toInt();
    }
    if(ui->lineEdit3->text()!=""){
        ss = ui->lineEdit3->text().toInt();
    }
    if(!QTime::isValid(hh, mm, ss)){
        QString str("输入的时间不合法,请确认");
        speaker->say(str);
        QMessageBox::critical(this,"提示",str);
        return;
    }
    this->qTime = new QTime(hh,mm,ss);
    ui->label->setText(qTime->toString("hh:mm:ss"));
    speaker->say(QString("操作成功,您设置的闹钟为%1点%2分%3秒").arg(hh).arg(mm).arg(ss));
    ui->pushButton1->setEnabled(false);
    timer->start(1000);//开启定时器
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值