//clock_exercise.cpp#include "clock_timer.h"
#include "ui_clock_timer.h"//时间事件处理函数
void Clock_Timer::timerEvent(QTimerEvent *event)
{if(event->timerId() == time_id){sys_tm = QDateTime :: currentDateTime();
// int year = sys_tm.date().year();
// int month = sys_tm.date().month();
// int day = sys_tm.date().day();//时分秒int hour = sys_tm.time().hour();int min = sys_tm.time().minute();int sec = sys_tm.time().second();
// qDebug() << "系统时间:" << hour << ":" << min << ":" << sec;QString settime = timeEdit->text();this->timeLab->setText(sys_tm.toString("yyyy-mm-dd hh:mm:ss"));splitTime();QString readtxt =this->txt_edit->toPlainText();vector<int> :: iterator iter = split.begin();if(*(iter++) == hour && *(iter++) == min && *iter == sec){speech->say(this->txt_edit->toPlainText());}}
}Clock_Timer::Clock_Timer(QWidget *parent) :QWidget(parent),ui(new Ui::Clock_Timer)
{ui->setupUi(this);this->resize(800,700);this->setWindowFlags(Qt :: FramelessWindowHint);
// this->setWindowTitle("小淼子快起床了");//显示系统时间标签timeLab = new QLabel(this);timeLab->move(30,20);timeLab->resize(300,60);timeLab->setText("系统时间");timeLab->setFont(QFont("楷体",16));timeLab->setStyleSheet("background-color:skyblue");//启动按钮startBtn = new QPushButton(this);startBtn->setText("启动");startBtn->move(600,50);startBtn->resize(QSize(60,40));connect(startBtn,&QPushButton :: clicked,this,&Clock_Timer :: startRecTime);//停止按钮stopBtn = new QPushButton(this);stopBtn->resize(startBtn->size());stopBtn->move(startBtn->x() + startBtn->width() + 20,startBtn->y());stopBtn->setText("停止");stopBtn->setEnabled(false);connect(stopBtn,&QPushButton :: clicked,this,&Clock_Timer :: stopRecTime);//用户输入时间单行编辑器timeEdit = new QLineEdit(this);timeEdit->move(400,timeLab->y());timeEdit->setFont(QFont("楷体",16));timeEdit->resize(200,80);timeEdit->setPlaceholderText("时:分:秒");//用户输入语音播报文本编辑器txt_edit = new QTextEdit(this);txt_edit->move(0,200);txt_edit->resize(QSize(this->width(),this->height() - 200));txt_edit->setFont(QFont("楷体",20));//语音播报speech = new QTextToSpeech(this);}void Clock_Timer::mouseMoveEvent(QMouseEvent *event)
{if(event->buttons() == Qt :: LeftButton){this->move(event->globalPos() - startPos);}}void Clock_Timer::mousePressEvent(QMouseEvent *event)
{if(event->buttons() == Qt :: LeftButton){this->startPos = event->globalPos() - this->pos();}else if(event->buttons() == Qt :: RightButton){int ret = QMessageBox :: question(this,"确认","是否要关闭界面",QMessageBox :: Yes | QMessageBox :: No,QMessageBox :: No);if(ret == QMessageBox :: Yes){this->close();}}}Clock_Timer::~Clock_Timer()
{delete ui;
}//按下启动按钮后的事件处理函数
void Clock_Timer::startRecTime()
{
// qDebug() << this->timeEdit->text();if(this->timeEdit->text() == ""){QMessageBox :: warning(this,"警告","无输入",QMessageBox :: Ok,QMessageBox :: Ok);return;}//点击启动后,解锁停止按钮,同时锁定启动按钮this->stopBtn->setEnabled(true);this->startBtn->setEnabled(false);this->timeEdit->setEnabled(false);this->txt_edit->setEnabled(false);//开启定时器time_id = this->startTimer(1000);
}//按下停止按钮后的事件处理函数
void Clock_Timer::stopRecTime()
{int ret = QMessageBox :: question(this,"提示","是否确认要停止定时器",QMessageBox :: Yes | QMessageBox :: No,QMessageBox :: Yes);if(ret == QMessageBox :: Yes){this->startBtn->setEnabled(true);this->stopBtn->setEnabled(false);this->timeEdit->setEnabled(true);this->txt_edit->setEnabled(true);//关闭定时器this->killTimer(time_id);}}//分割时分秒
void Clock_Timer :: splitTime()
{this->split.clear();QString temp = timeEdit->text();QStringList s = temp.split(":");QStringList :: iterator iter = s.begin();for(;iter != s.end();iter++){qDebug() << (*iter);split.push_back(iter->toInt());}
}
//头文件#ifndef CLOCK_TIMER_H
#define CLOCK_TIMER_H#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QTime>
#include <QTextToSpeech>
#include <QDateTime>
#include <QMessageBox>
#include <vector>
#include <QDebug>
#include <QMouseEvent>using namespace std;
namespace Ui {
class Clock_Timer;
}class Clock_Timer : public QWidget
{Q_OBJECTvoid timerEvent(QTimerEvent *event); //时间事件处理函数public:explicit Clock_Timer(QWidget *parent = nullptr);void mouseMoveEvent(QMouseEvent *event);void mousePressEvent(QMouseEvent *event);~Clock_Timer();void splitTime();
public slots:void startRecTime();void stopRecTime();private:Ui::Clock_Timer *ui;QPushButton *startBtn; //启动按钮QPushButton *stopBtn; //停止按钮QLabel *timeLab; //系统获取时间QLineEdit *timeEdit; //记录用户输入的时间QTextEdit *txt_edit; //用户输入的播报内容QMessageBox *stopConfirm; //对话框,询问用户是否确认停止QTextToSpeech *speech;int time_id; //记录定时器IDQDateTime sys_tm; //记录系统时间vector<int> split;QPoint startPos; //记录鼠标起始点};#endif // CLOCK_TIMER_H
运行效果