1.完成简易闹钟
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTextToSpeech> //播报类
#include <QTimer> //定时器类
#include <QTime> //时间类QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();//重写定时器事件处理函数void timerEvent(QTimerEvent *event) override;private slots:void on_btn1_clicked();void on_btn2_clicked();private:Ui::Widget *ui;QTextToSpeech *s ;//定义定时器指针QTimer *t;//定义整形变量,存放定时器idint t_id1;int t_id2;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//实例化一个播报员s = new QTextToSpeech(this);//执行启动逻辑t_id1 = this->startTimer(1000);this->ui->btn2->setEnabled(false);
}Widget::~Widget()
{delete ui;
}void Widget::timerEvent(QTimerEvent *e)
{//可以使用event的成员函数timerId()来获取已经到位的定时器标识符if (e->timerId() == t_id1) //处理系统时间{//获取系统时间QTime sys_time1 = QTime::currentTime();//将时间转为字符串QString time1 = sys_time1.toString("hh:mm:ss");//将字符串展示到ui界面ui->systimelab->setText(time1);ui->systimelab->setAlignment(Qt::AlignCenter);}if (e->timerId() == t_id2) //处理设置时间{//获取系统时间QTime sys_time2 = QTime::currentTime();//将时间转为字符串QString time2 = sys_time2.toString("hh:mm:ss");if (ui->settime->text() == time2){//调用该播报员的成员函数 says->say(ui->textedt->toPlainText());}}
}void Widget::on_btn1_clicked()
{//执行开启逻辑t_id2 = this->startTimer(500);this->ui->btn2->setEnabled(true);this->ui->btn1->setEnabled(false);this->ui->settime->setEnabled(false);this->ui->textedt->setEnabled(false);
}void Widget::on_btn2_clicked()
{//执行关闭逻辑s->stop();this->killTimer(t_id2);this->ui->settime->clear();this->ui->textedt->clear();this->ui->btn2->setEnabled(false);this->ui->btn1->setEnabled(true);this->ui->settime->setEnabled(true);this->ui->textedt->setEnabled(true);
}
2.发布软件
3.思维导图