实现一个闹钟
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//this->resize(600,400);
this->setWindowTitle("闹钟");
this->setFixedSize(820,600);
lab = new QLabel(this);
lab->resize(350,150);
lab->move(70,70);
lab->setStyleSheet("background-color:green");
ledit = new QLineEdit(this);
ledit->resize(320,70);
ledit->move(440,70);
ledit->setFont(QFont("宋体",20));
startbtn = new QPushButton("启动",this);
startbtn->resize(145,55);
startbtn->move(440,160);
stopbtn = new QPushButton("停止",this);
stopbtn->resize(145,55);
stopbtn->move(610,160);
tedit=new QTextEdit("三更灯火五更鸡,"
"正式男儿读书时;"
"黑发布置勤学早,"
"白首方悔读书迟。"
,this);
tedit->setFont(QFont("宋体",20));
tedit->resize(700,250);
tedit->move(70,300);
timer.start(1000);
connect(&timer,&QTimer::timeout,this,&Widget::lab_datatime);
connect(startbtn,&QPushButton::clicked,this,&Widget::startbtn_slots);
connect(stopbtn,&QPushButton::clicked,this,&Widget::stop_slots);
speecher = new QTextToSpeech;
}
Widget::~Widget()
{
}
void Widget::lab_datatime()
{
QDateTime sysdati=QDateTime::currentDateTime();
QString datatime=sysdati.toString(" yyyy - MM - dd \n"
" hh : mm : ss");
this->lab->setText(datatime);
this->lab->setFont(QFont("宋体",20));
}
void Widget::startbtn_slots()
{
if(startbtn->text()=="启动")
{
tid=this->startTimer(1000);
}
}
void Widget::stop_slots()
{
this->killTimer(tid);
}
void Widget::timerEvent(QTimerEvent* event)
{
QDateTime sysdati=QDateTime::currentDateTime();
QString datatime=sysdati.toString(" hh : mm : ss");
QString setdati=ledit->text();
if(datatime==setdati)
{
QString s2="现在是北京时间";
QString s3="点整";
speecher->say(QString("%1,%2,%3").arg(s2).arg(sysdati.toString("hh")).arg(s3));
}
}