实现闹钟功能
1》 头文件
#ifndef BURGER_H
#define BURGER_H#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTextEdit>
#include <QTimerEvent> //定时器事件类
#include <QDateTime>
#include <QTextToSpeech>
#include <QMouseEvent>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class burger; }
QT_END_NAMESPACEclass burger : public QWidget
{Q_OBJECTpublic:burger(QWidget *parent = nullptr);~burger();void timerEvent (QTimerEvent *event)override;
private slots:void end_cli();void start_cli();
private:Ui::burger *ui;QLabel *back;QTextEdit *text;QPushButton *startbtn;QPushButton *endbtn;QLineEdit *enterline;QLabel *timelabel;int time_t;QTextToSpeech *speech;
};
#endif // BURGER_H
2》源文件:
#include "burger.h"
#include "ui_burger.h"burger::burger(QWidget *parent): QWidget(parent), ui(new Ui::burger)
{ui->setupUi(this);//窗口设置this->resize(540,360);this->setWindowTitle("Cecilla's alarm");this->setWindowIcon(QIcon(":/IMG_0895.JPG"));this->setWindowFlags(Qt::FramelessWindowHint);//背景设置back = new QLabel(this);back->resize(this->size());back->setPixmap(QPixmap(":/IMG_0092.jpg"));back->setScaledContents(true);//文本框text = new QTextEdit(this);text->resize(260,160);text->move(80,180);//启动按钮startbtn = new QPushButton("启动",this);startbtn->resize(80,50);startbtn->move(300,100);//关闭按钮endbtn = new QPushButton("关闭",this);endbtn->resize(80,50);endbtn->move(420,100);//输入时间文本框enterline = new QLineEdit(this);enterline->resize(200,50);enterline->move(300,40);//时间显示文本框timelabel = new QLabel(this);timelabel->resize(220,60);timelabel->move(20,60);timelabel->setStyleSheet("background-color:pink;");time_t = this->startTimer(1000);//语音播报speech = new QTextToSpeech(this);connect(endbtn,&QPushButton::clicked,this,&burger::end_cli);connect(startbtn,&QPushButton::clicked,this,&burger::start_cli);
}burger::~burger()
{delete ui;
}
void burger::timerEvent(QTimerEvent *event)
{if(event->timerId() == time_t){QDateTime sys_date=QDateTime::currentDateTime(); //获取当前系统日期时间//时间转换为字符串QString t=sys_date.toString("yyyy:MM:dd hh-mm-ss");//输出显示this->timelabel->setText(t);this->timelabel->setAlignment(Qt::AlignCenter); //文本对齐this->timelabel->setFont(QFont("微软雅黑",13));QString s=sys_date.toString("hh:mm:ss");if(s==enterline->text() && startbtn->text()=="已设置"){speech->say(text->toPlainText());}}
}void burger::end_cli()
{this->close();
}void burger::start_cli()
{QString s = enterline->text();if(s == ""){enterline->setText("请输入时间");}if(startbtn->text()== "启动"){startbtn->setText("已设置");}else{startbtn->setText("启动");}
}
思维导图:::