当前位置: 首页> 文旅> 旅游 > 9.11 QT ( Day 4)

9.11 QT ( Day 4)

时间:2025/8/23 15:21:18来源:https://blog.csdn.net/weixin_69367386/article/details/142145230 浏览次数:0次

一、作业

 

1.Widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTimerEvent> //定时器类
#include <QTime>
#include <QtTextToSpeech> //文本转语音类QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void timerEvent(QTimerEvent *e);
signals:void  my_signal();  //定义一个信号,只要声明,不用实现
private slots:void on_startBtn_clicked();private:Ui::Widget *ui;int  id;QTextToSpeech *speecher; //实例化一个语音播报者
};
#endif // WIDGET_H

2.Widget.cpp

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget),speecher(new QTextToSpeech(this))
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}//重写定时器事件函数的实现
void Widget::timerEvent(QTimerEvent *e)
{if(e->timerId() == id){QTime sys_time = QTime::currentTime();QString t = sys_time.toString("hh:mm:ss");//将系统时间放入lab中ui->timeLab->setText(t);if( t == ui->timerEdit->text()) //当填入的时间等于实际时间时,闹钟播报sayLab内的内容{for(int num = 5; num > 0; num--) //到了闹钟定的时间时,播报5次停止{speecher->say(ui->sayLab->text());}}}
}//点击启动后运行的槽函数
void Widget::on_startBtn_clicked()
{if(ui->startBtn->text() == "启动"){id = startTimer(1000);ui->timeLab->setAlignment(Qt::AlignCenter);}}

个人效果图:

二、思维导图

关键字:9.11 QT ( Day 4)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: