Qt day4

闹钟定时器

 右上角设置时间,当时间到了之后语音播报文本框中设置的内容

代码:

widget.h


#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QTimer>              //定时器类
#include<QTime>            //时间类
#include <QDebug>         //debug
#include<QTimerEvent>          //定时器事件类
#include<QDateTime>           //日期时间类
#include <QtTextToSpeech>//语音播报

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget

{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
protected:
    void timerEvent(QTimerEvent *event)override;//重写定时器处理函数
private slots:
    void on_startBtn_clicked();

    void on_closeBtn_clicked();

private:
    Ui::Widget *ui;
    //使用定时器类,定义一个定时器指针
    QTimer *timer;
    //定义一个整形变量,记录启动的定时器
    int event_timer;//闹钟定时器
    int uitimer;//时间
    QTextToSpeech *speech;//语音播报
};

#endif // WIDGET_H

wdiget.cpp


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


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("闹钟计时器");
    ui->closeBtn->setEnabled(false);//默认关闭不可点击
    QFont my_size;
    my_size.setPixelSize(30);//设置时钟大小
    ui ->nowTime ->setAlignment(Qt::AlignCenter);
    ui ->nowTime ->setFont(my_size);

    uitimer=this->startTimer(1);//一毫秒更新一次时间
    ui->setTime->setPlaceholderText("闹钟格式 hh:mm (24小时制)");
    ui->textEdit->setPlaceholderText("请输入播报内容...");
    speech = new QTextToSpeech(this);//初始化语音播报

}

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

void Widget::on_startBtn_clicked()
{
    if(ui->setTime->text()==""){
        speech->say("测试语音");//测试
        speech->say(ui->textEdit->toPlainText());//播报输入框内的东西
        return;
    }
    //将本按钮设置为不可点击
    ui->startBtn->setEnabled(false);
    //将关闭按钮设置为可以点击
    ui->closeBtn->setEnabled(true);
    //执行功能
    //speech->say("测试语音");
    //qDebug()<<ui->setTime->text();
    event_timer=this->startTimer(1000);//设置定时器一秒钟检查一次
}


void Widget::on_closeBtn_clicked()
{
    //将本按钮设置为不可点击
    ui->closeBtn->setEnabled(false);
    //将启动按钮设置为可以点击
    ui->startBtn->setEnabled(true);
    //执行功能
    killTimer(event_timer);//关闭定时器
}

//重写定时器处理函数
void Widget::timerEvent(QTimerEvent *event){
    QTime t =QTime::currentTime();//获取系统时间
    if(event->timerId()==uitimer){
        QString nowtime=t.toString("hh:mm:ss a");//时间格式
        ui->nowTime->setText(nowtime);//设置ui时间
    }
    if(event->timerId()==event_timer){
        if(t.toString("hh:mm")==ui->setTime->text()){
            speech->say(ui->textEdit->toPlainText());//播报输入框内的东西
            killTimer(event_timer);//关闭定时器
            //将关闭按钮设置为不可点击
            ui->closeBtn->setEnabled(false);
            //将启动按钮设置为可以点击
            ui->startBtn->setEnabled(true);
        }
    }
}

界面:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值