2023年12月12日作业

头文件

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 *e);//对基类中虚函数进行重写

private slots:



    void on_Btn_clicked();

private:
    Ui::Widget *ui;

    int id,id1;

    QTextToSpeech *speecher;//定义语音播报者
};
#endif // WIDGET_H

源文件

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->clock_timeLab->setAlignment(Qt::AlignCenter);//居中显示
    //启动一个定时器
    id = startTimer(1000);
    speecher = new QTextToSpeech(this);//给语音播报者实例化空间
}

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

void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId()==id)
    {
        QTime system_time = QTime::currentTime();//获取系统当前时间
        QString s = system_time.toString("hh:mm:ss");//将系统时间转换
        ui->sys_timeLab->setText(s);//将系统时间放入到label中
        ui->sys_timeLab->setAlignment(Qt::AlignCenter);//居中显示
    }
    else if(e->timerId()==id1)
    {
        if(ui->lineEdit->text()==ui->sys_timeLab->text())//判断条件
        {
            ui->label->setText("吃饱啦");
            ui->label->setAlignment(Qt::AlignCenter);
            for(int i=0;i<5;i++)
            {
                speecher->say(ui->label->text());//语音播报者播报文本内容
            }
              ui->Btn->setText("启动");
        }

    }
}
//按钮点击的槽函数
void Widget::on_Btn_clicked()
{
    id1 = startTimer(1000);//启动一个定时器
    ui->Btn->setText("已启动");//更改文本直观看到是否启动
}

效果图

思维导图

相关推荐
blasit8 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_1 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星2 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马5 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝5 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc5 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法