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("启动");
    }
}
相关推荐
向阳@向远方9 分钟前
第二章 简单程序设计
开发语言·c++·算法
Mr_Xuhhh40 分钟前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构
纳兰青华1 小时前
bean注入的过程中,Property of ‘java.util.ArrayList‘ type cannot be injected by ‘List‘
java·开发语言·spring·list
好开心啊没烦恼1 小时前
Python 数据分析:DataFrame,生成,用字典创建 DataFrame ,键值对数量不一样怎么办?
开发语言·python·数据挖掘·数据分析
liulilittle1 小时前
VGW 虚拟网关用户手册 (PPP PRIVATE NETWORK 基础设施)
开发语言·网络·c++·网关·智能路由器·路由器·通信
Devil枫1 小时前
Kotlin高级特性深度解析
android·开发语言·kotlin
ChinaDragonDreamer1 小时前
Kotlin:2.1.20 的新特性
android·开发语言·kotlin
安之若素^1 小时前
启用不安全的HTTP方法
java·开发语言
一个天蝎座 白勺 程序猿2 小时前
Python(28)Python循环语句指南:从语法糖到CPython字节码的底层探秘
开发语言·python
持梦远方2 小时前
C 语言基础入门:基本数据类型与运算符详解
c语言·开发语言·c++