qt实现设定一个简易闹钟

 

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

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

    //实例化一个定时器对象
    t1 = new QTimer(this);

    //开启一个定时器记录系统时间
    tid_1 = this->startTimer(1000);

    //实例化一个播报者对象
    speech = new QTextToSpeech(this);

    //初始化关闭按钮不可用
    ui->bnt_close->setEnabled(false);

}

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

//重写定时器处理函数
void Widget::timerEvent(QTimerEvent *e)
{
    //获取系统当前时间
    QDateTime sys_dt = QDateTime::currentDateTime();

    //将系统时间转化为字符串
    QString dt = sys_dt.toString("hh:mm:ss");

    //将日期显示在ui界面上
    ui->labtime->setAlignment(Qt::AlignCenter);
    ui->labtime->setText(dt);

    //实时判断时间是否相等
    if(e->timerId() == tid_2)
    {
        //将linetime内容转为字符串,判断与系统时间是否相同,相同则播报
        QString set_dt = ui->linetime->text();
        if(set_dt == dt)
        {
            speech->say(ui->textEdit->toPlainText());
        }
    }

}

//按钮触发事件
void Widget::on_bnt_start_clicked()
{
    //启动定时器
    tid_2 = this->startTimer(1000);

    //将按钮1与linetime设为不可用状态
    ui->bnt_start->setEnabled(false);
    ui->linetime->setEnabled(false);
    ui->bnt_close->setEnabled(true);

}

void Widget::on_bnt_close_clicked()
{
    //    ui->bnt_start->setEnabled(true);
    //    ui->linetime->setEnabled(true);
    this->close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值