闹钟
头文件
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTimerEvent> //定时器事件处理函数
#include <QTime> //时间类
#include <QString>
#include <QPushButton>
#include <QTextToSpeech>
#include <QMessageBox>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;int tid;QTextToSpeech *speecher;};
#endif // WIDGET_H
源文件
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//启动定时器tid = startTimer(1000);//让停止按钮默认不可用ui->stopbtn->setEnabled(false);//构造演讲者speecher = new QTextToSpeech(this);//窗口标题this->setWindowTitle("clock");this->setWindowIcon(QIcon(":/C:/Users/PC/Desktop/my/my_icon.png"));//设置透明度this->setWindowOpacity(0.9);}Widget::~Widget()
{delete ui;
}void Widget::on_startbtn_clicked()
{//将启动按钮,两个编辑器不可用,停止按钮可用ui->stopbtn->setEnabled(true);ui->startbtn->setEnabled(false);ui->clocktxtedit->setEnabled(false);ui->clocktimeedit->setEnabled(false);
}void Widget::on_stopbtn_clicked()
{int res = QMessageBox::warning(this,"警告","确定停止吗",QMessageBox::Yes | QMessageBox::No);//将启动按钮,两个编辑器可用,停止按钮不可用if(res == QMessageBox::Yes){ui->stopbtn->setEnabled(false);ui->startbtn->setEnabled(true);ui->clocktxtedit->setEnabled(true);ui->clocktimeedit->setEnabled(true);ui->clocktimeedit->clear();}
}void Widget::timerEvent(QTimerEvent *e)
{if(e->timerId() == tid){//获取系统时间QTime sys_time = QTime::currentTime();//将时间转换为字符串QString t = sys_time.toString("hh:mm:ss");//将字符串展示到ui界面ui->systimelab->setText(t);ui->systimelab->setAlignment(Qt::AlignCenter);//判断系统时间是否和设定时间相同if(ui->systimelab->text() == ui->clocktimeedit->text()){speecher->say(ui->clocktxtedit->toPlainText());}}
}
思维导图