嵌入式学习-qt-Day4
一、思维导图
二、作业
1.设计一个界面:显示系统时间;可以设置闹钟,在设置的时间到达后,显示五次字符串,并且语音播报。
Wight.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QDebug>
#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) override;
int t1;//定时器1
int t2;//定时器2
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
QTextToSpeech *speech;//语音播报者
};
#endif // WIDGET_H
Wight.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
t1 = startTimer(1000);//启动定时器t1
speech = new QTextToSpeech(this); //实例化语音播报者
}
//定时器函数
void Widget::timerEvent(QTimerEvent *e)
{
//系统时间
if (e->timerId() == t1)
{
QTime sys_time = QTime::currentTime();
QString s = sys_time.toString("hh:mm:ss");
ui->sys_time->setText(s);
}
//闹钟
if ((e->timerId() == t2))
{
for(int i = 0;i < 5; i++){
ui->label->setText("好好学习天天向上");
speech->say(ui->label->text());
}
killTimer(t2);//关闭定时器2
}
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
QTime sys_time = QTime::currentTime();
QTime alarm_time = ui->timeEdit->time();
int current_s = sys_time.hour()*3600 + sys_time.minute()*60 + sys_time.second();
int alarm_s = alarm_time.hour()*3600 + alarm_time.minute()*60 + alarm_time.second();
t2 = startTimer((alarm_s - current_s)*1000);//启动定时器2
ui->label->setText("设置成功!");
}
运行结果