Qt 12.30 day5

复制代码
day5_testppp.pro
cpp 复制代码
QT       +=core gui texttospeech
复制代码
widget.h
cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimerEvent>//定时器事件类
#include <QTimer>//时间事件类
#include <QTime>//时间类
#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 *event);

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    int id;
    QTime sys_time;
    bool flag = false;
    QTextToSpeech *speecher;
};
#endif // WIDGET_H

widget.cpp

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    ,speecher(new QTextToSpeech(this))
{
    ui->setupUi(this);
    id = startTimer(1000);
}

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

//重写定时器事件函数的实现
void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == id)
    {
        //获取系统时间 QTime
        QTime sys_time = QTime::currentTime();
        //将系统事件转换为字符串
        QString t = sys_time.toString("hh--mm--ss");
        //将系统时间放入lab中
        ui->label->setText(t);
        //居中
        ui->label->setAlignment(Qt::AlignCenter);
        //比较label中的系统时间和设定的闹钟时间是否一致
        if(ui->label->text()==ui->lineEdit->text()&&flag==true)
        {
            //语音播放5遍
            for(int i=0;i<5;i++)
            {
                speecher->say(ui->label_3->text());

            }
            //播完五遍语音按钮复位
            ui->pushButton->setText("启动");
        }
    }
}


//启动按钮对应的槽函数
void Widget::on_pushButton_clicked()
{
    //实现一钮两用
    if(ui->pushButton->text() == "启动")
    {
        //设置flag的值为true;
        flag = true;
        //按钮变为关闭
        ui->pushButton->setText("关闭");

    }
    else{
        //设置flag为flase
        flag = false;
        //复位按钮
        ui->pushButton->setText("启动");
    }
}
相关推荐
苦夏木禾16 分钟前
js请求避免缓存的三种方式
开发语言·javascript·缓存
超级土豆粉24 分钟前
Turndown.js: 优雅地将 HTML 转换为 Markdown
开发语言·javascript·html
wei_shuo1 小时前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai
熊猫钓鱼>_>1 小时前
用Python解锁图像处理之力:从基础到智能应用的深度探索
开发语言·图像处理·python
GO兔2 小时前
开篇:GORM入门——Go语言的ORM王者
开发语言·后端·golang·go
好开心啊没烦恼2 小时前
Python 数据分析:numpy,抽提,整数数组索引与基本索引扩展(元组传参)。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy·pandas
future14123 小时前
C#学习日记
开发语言·学习·c#
king_harry3 小时前
Java程序-OceanBase Connector/J 示例
开发语言
傻啦嘿哟4 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#
翻滚吧键盘4 小时前
js代码09
开发语言·javascript·ecmascript