作业要求:
1> 思维导图
2>
作业1:
作业2:
运行代码:
main.cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
#include <QTimerEvent>
#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_btn1_clicked();
private:
Ui::Widget *ui;
int id;
QString alarm;
QString song;
};
#endif // WIDGET_H
Widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
,song("你怎么睡得着哒,你这个年龄段,你这个年龄段睡得着觉,有点出息没有")
{
ui->setupUi(this);
id = startTimer(1000);
ui->lineEdit1->setPlaceholderText("hh/mm");
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
QTime sys_time = QTime::currentTime();
//设置闹钟的多种书写情况,规定/为分隔符
QString s1 = sys_time.toString("hh:mm:ss");
QString s2 = sys_time.toString("hh/mm:ss");
QString s3 = sys_time.toString("h/m:ss");
QString s4 = sys_time.toString("hh/m:ss");
QString s5 = sys_time.toString("h/mm:ss");
ui->lab1->setText(s1);
static int num=0;
static int flag = -1;
//如果到时间:00秒,置flag==1;表示闹钟开始
if(((s1==alarm+"00")|s2==(alarm+":00")|(s3==alarm+"00")|s4==(alarm+":00")|s5==(alarm+":00"))&&(flag==-1)){
flag = 1;
}
ui->lab3->clear();
//flag==1时,闹钟开始
if(flag==1){
//num<=5,表示闹钟响五次
num++;
if(num<=5){
if(num==0){
ui->lab3->setText(song+"\n");
}
ui->lab3->setText(ui->lab3->text()+song+"\n");
QTextToSpeech *speechr = new QTextToSpeech(this);
speechr->say(song);
}else{
//闹钟响五次后,重置
ui->btn1->setText("启动");
ui->lineEdit1->setPlaceholderText("hh:mm");
//置flag=0 表示闹钟关闭 次数置0 清空框
flag=0;
num=0;
}
}
}
//按钮的槽函数
void Widget::on_btn1_clicked()
{
//设置闹钟
alarm = ui->lineEdit1->text();
//当line为非空,按键为关闭,开启闹钟
if(alarm.length()!=0){
//设置闹钟为关闭 清空 设置占位符
ui->btn1->setText("关闭");
ui->lineEdit1->clear();
ui->lineEdit1->setPlaceholderText(alarm);
}else if(ui->btn1->text()=="关闭"){
//点击关闭后,关闭闹钟 设置占位符
ui->btn1->setText("启动");
ui->lineEdit1->setPlaceholderText("hh:mm");
}
qDebug()<<"启动";
}
Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
#include <QTimerEvent>
#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_btn1_clicked();
private:
Ui::Widget *ui;
int id;
QString alarm;
QString song;
};
#endif // WIDGET_H