闹钟项目:
widget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QTimerEvent> #include <QTime> #include <QDebug> #include <QTextToSpeech> #include <QMessageBox> #include <QTimer>QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACEclass Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void timerEvent(QTimerEvent *e); //要重写的关于定时器时间处理函数的声明private slots:void on_startBtn_clicked();void on_stopBtn_clicked();private:Ui::Widget *ui;QTextToSpeech *speecher;QTimer *timer;int tId; }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);ui->hangEdit->setText("20-20-20");tId = startTimer(1000);this->timer = new QTimer;speecher = new QTextToSpeech(this);}Widget::~Widget() {delete ui;}void Widget::timerEvent(QTimerEvent *e) {if(e->timerId()==tId){//获取系统时间QTime sys_time = QTime::currentTime(); //QTime类对象//将时间转化为字符串QString t = sys_time.toString("hh-mm-ss");//将字符串展示到ui界面ui->syslabel->setText(t);ui->syslabel->setAlignment(Qt::AlignCenter);if(ui->syslabel->text()==ui->hangEdit->text()){speecher->say(ui->tEdit->toPlainText());}} }void Widget::on_startBtn_clicked() {if(ui->hangEdit->text()== NULL){QMessageBox::critical(this,"错误","请先编辑时间",QMessageBox::Ok);}return;timer->start(100);ui->hangEdit->setReadOnly(true);ui->tEdit->setReadOnly(true);}void Widget::on_stopBtn_clicked() {int ret = QMessageBox::question(this,"提示","确定要停止闹钟吗?",QMessageBox::Yes | QMessageBox::No);if(ret = QMessageBox::Yes){timer->stop();ui->hangEdit->setReadOnly(false);ui->tEdit->setReadOnly(false);ui->hangEdit->clear();}}
思维导图: