定时闹钟
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
#include <QIntValidator>
#include <QTime>
#include <QMessageBox>
#include <QTextToSpeech>
#include <QTimer>
#include <QMouseEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public slots:
void checkTime();
public:
Widget(QWidget *parent = nullptr);
~Widget();
QTime *qTime;
QTextToSpeech *speaker;
QTimer *timer;
QPoint startPoint;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
private slots:
void on_pushButton1_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
void Widget::checkTime()
{
QTime curr = QTime::currentTime();
if(curr.toString("hh:mm:ss") == this->qTime->toString("hh:mm:ss")){
QString str = ui->textEdit1->toPlainText();
if(str == ""){
str = "时间已到";
}
speaker->say(str);
QMessageBox::information(this,"提示","时间已到");
this->close();
}
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(QSize(800,500));
this->setWindowFlag(Qt::FramelessWindowHint);
this->speaker = new QTextToSpeech(this);
//闹钟的时 分 秒设置校验器
QIntValidator *validator1 = new QIntValidator(0,23,ui->lineEdit1);
QIntValidator *validator2 = new QIntValidator(0,59,ui->lineEdit2);
QIntValidator *validator3 = new QIntValidator(0,59,ui->lineEdit3);
ui->lineEdit1->setValidator(validator1);
ui->lineEdit2->setValidator(validator2);
ui->lineEdit3->setValidator(validator3);
ui->lineEdit1->setInputMethodHints(Qt::ImhDigitsOnly);
ui->lineEdit2->setInputMethodHints(Qt::ImhDigitsOnly);
ui->lineEdit3->setInputMethodHints(Qt::ImhDigitsOnly);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, QOverload<>::of(&Widget::checkTime));
}
Widget::~Widget()
{
delete ui;
}
void Widget::mousePressEvent(QMouseEvent *event)
{
startPoint = event->globalPos() - this->frameGeometry().topLeft();
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
this->move(event->globalPos() - startPoint );
}
void Widget::on_pushButton1_clicked()
{
int hh = -1,mm=-1,ss=-1;
if(ui->lineEdit1->text()!=""){
hh = ui->lineEdit1->text().toInt();
}
if(ui->lineEdit2->text()!=""){
mm = ui->lineEdit2->text().toInt();
}
if(ui->lineEdit3->text()!=""){
ss = ui->lineEdit3->text().toInt();
}
if(!QTime::isValid(hh, mm, ss)){
QString str("输入的时间不合法,请确认");
speaker->say(str);
QMessageBox::critical(this,"提示",str);
return;
}
this->qTime = new QTime(hh,mm,ss);
ui->label->setText(qTime->toString("hh:mm:ss"));
speaker->say(QString("操作成功,您设置的闹钟为%1点%2分%3秒").arg(hh).arg(mm).arg(ss));
ui->pushButton1->setEnabled(false);
timer->start(1000);//开启定时器
}