8.20 QT

1.思维导图

2.

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTime>
#include <QTimerEvent>
#include <QTextToSpeech>

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 *e);

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;

    int start_id;

    int sys_id;

    QTextToSpeech *speecher;
};
#endif // WIDGET_H

源文件

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

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

    //启动一个定时器
    sys_id = startTimer(1000);
}

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

void Widget::timerEvent(QTimerEvent *e)
{
    //判断是否是sys_id超时
    if(e->timerId() == sys_id)
    {
        QTime sys = QTime::currentTime();

        ui->label->setText(sys.toString("hh:mm:ss"));
    }
    //判断是否是start_id超时
    if(e->timerId() == start_id)
    {
        if(ui->lineEdit->text() == ui->label->text())
        {
            for (int i = 0;i<6;i++)
            {
                 speecher->say(ui->label_3->text());
            }
        }
    }

}

void Widget::on_pushButton_clicked()
{
    if(ui->pushButton->text() == "启动")
       {
           //启动一个定时器
           start_id = startTimer(1000); //每隔1秒钟,自动执行timerEvent()函数


           //将文本设置“关闭”
           ui->pushButton->setText("关闭");
       }
       else
       {
            //关闭定时器
            killTimer(start_id);

            speecher->stop();

           //将文本设置“启动”
           ui->pushButton->setText("启动");
       }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值