Qt : day3

1.完成简易闹钟

 

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTextToSpeech>    //播报类
#include <QTimer>   //定时器类
#include <QTime>    //时间类

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    //重写定时器事件处理函数
    void timerEvent(QTimerEvent *event) override;

private slots:
    void on_btn1_clicked();

    void on_btn2_clicked();

private:
    Ui::Widget *ui;

    QTextToSpeech *s ;

    //定义定时器指针
    QTimer *t;

    //定义整形变量,存放定时器id
    int t_id1;
    int t_id2;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"

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

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

    //执行启动逻辑
    t_id1 = this->startTimer(1000);

    this->ui->btn2->setEnabled(false);
}

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

void Widget::timerEvent(QTimerEvent *e)
{
       //可以使用event的成员函数timerId()来获取已经到位的定时器标识符
       if (e->timerId() == t_id1)      //处理系统时间
       {
           //获取系统时间
           QTime sys_time1 = QTime::currentTime();

           //将时间转为字符串
           QString time1 = sys_time1.toString("hh:mm:ss");

           //将字符串展示到ui界面
           ui->systimelab->setText(time1);
           ui->systimelab->setAlignment(Qt::AlignCenter);
       }
       if (e->timerId() == t_id2)       //处理设置时间
       {
           //获取系统时间
           QTime sys_time2 = QTime::currentTime();

           //将时间转为字符串
           QString time2 = sys_time2.toString("hh:mm:ss");

           if (ui->settime->text() == time2)
           {
               //调用该播报员的成员函数 say
               s->say(ui->textedt->toPlainText());
           }
       }
}

void Widget::on_btn1_clicked()
{
    //执行开启逻辑
    t_id2 = this->startTimer(500);
    this->ui->btn2->setEnabled(true);
    this->ui->btn1->setEnabled(false);
    this->ui->settime->setEnabled(false);
    this->ui->textedt->setEnabled(false);
}

void Widget::on_btn2_clicked()
{
    //执行关闭逻辑
    s->stop();
    this->killTimer(t_id2);
    this->ui->settime->clear();
    this->ui->textedt->clear();
    this->ui->btn2->setEnabled(false);
    this->ui->btn1->setEnabled(true);
    this->ui->settime->setEnabled(true);
    this->ui->textedt->setEnabled(true);
}

 2.发布软件

3.思维导图

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lin---

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值