# 2023/4/7 设置闹钟

2023/4/7 设置闹钟

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    t = new QTimer(this);

    //实例化一个播报员
    broadCaster = new QTextToSpeech(this);

    //将定时器发射的timeou信号,连接到自定义的槽函数中
    connect(t,&QTimer::timeout,this,&Widget::on_timeout);

    initCurrentTime();

}

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

//在currenTime中显示当前日期时间
void Widget::initCurrentTime()
{
    t = new QTimer(this);

    connect(t,&QTimer::timeout,this,&Widget::showCurrentTime);

    t->start(1000);

    showCurrentTime();
}

void Widget::showCurrentTime()
{

    //获取系统日期时间
    QDateTime sysDate = QDateTime::currentDateTime();

    //将日期时间转为字符串
    QString date = sysDate.toString("yyyy-MM-dd hh:mm:ss");

    //将字符串显示在ui界面
    ui->current_Time->setText(date);
    //居中显示
    ui->current_Time->setAlignment(Qt::AlignCenter);
}




void Widget::on_timeout()
{

    int hh,mm,ss;

    QStringList list = ui->set_alarm->text().split(":");
    hh = list[0].toUInt();
    mm = list[1].toUInt();
    ss = list[2].toUInt();
}


//非事件处理函数版对应的槽函数
void Widget::on_startBtn_clicked()
{
    if(ui->startBtn->text() == "启动")
    {
        t_id = this->startTimer(1000);
        ui->startBtn->setDisabled(true);  //点击后锁定启动按钮
        ui->stopBtn->setDisabled(false);  //点击后解锁关闭按钮
    }
}

void Widget::on_stopBtn_clicked()
{
    if(ui->stopBtn->text() == "关闭")
    {
        this->killTimer(t_id);
        ui->startBtn->setDisabled(false);//点击后锁定关闭按钮
        ui->stopBtn->setDisabled(true);  //点击后解锁启动按钮
    }
}


//重写定时器事件处理函数
void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == t_id)
    {
        //获取系统时间,将时间转为字符串

        QString time = QTime::currentTime().toString("hh:mm:ss");

        //获取闹钟定下的时间
        int hh,mm,ss;
        QStringList list_clock = ui->set_alarm->text().split(":");
        hh = list_clock[0].toUInt();
        mm = list_clock[1].toUInt();
        ss = list_clock[2].toUInt();

        qDebug()<< hh << " " << mm << "  " << ss;
        //比较闹钟时间与当前时间
        QStringList list = time.split(":");
        int h,m,s;
        h = list[0].toUInt();
        m = list[1].toUInt();
        s = list[2].toUInt();
        if(h == hh && m == mm && s == ss)
        {
            //播报语音
            broadCaster->say(ui->broadcast->toPlainText());
            qDebug()<<"成功";
            this->killTimer(t_id);
            ui->startBtn->setDisabled(false);//点击后锁定关闭按钮
            ui->stopBtn->setDisabled(true);  //点击后解锁启动按钮
        }
    }
}

//重写鼠标按压事件
void Widget::mousePressEvent(QMouseEvent *event)
{
    startPoint = event->globalPos() - this->frameGeometry().topLeft();
}

//重写鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos() - startPoint);
}


//重写绘画处理函数
void Widget::paintEvent(QPaintEvent *event)
{

}



在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值