widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
timer=new QTimer(this);
timer->start(1000);
Idtime=QObject::startTimer(1000);
ui->textEdit->setText("今天跟二狗子约好要干架,别忘了带好工具");
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_timer_timeout()
{
}
void Widget::on_btn1_clicked()
{
connect(timer,&QTimer::timeout,this,&Widget::on_btn1_clicked);
ui->btn1->setDisabled(true);
qDebug()<<"OK";
if(ui->lineEdit->text()==ui->label->text())
{
qDebug()<<"OK";
QTextToSpeech *speech=new QTextToSpeech(this);
QString text=ui->textEdit->toPlainText();
speech->say(text);
}
}
//重写定时器事件处理函数
void Widget::timerEvent(QTimerEvent *event)
{
QDateTime systime=QDateTime::currentDateTime();
QString time=systime.toString("yyyy MM dd hh:mm:ss");
ui->label->setText(time);
}
void Widget::on_btn2_clicked()
{
auto btn = QMessageBox::question(this,
"warning",
"确定是否退出闹钟模式?",
QMessageBox::Ok|QMessageBox::No);
//对用户选定的按钮,进行判断
if(btn==QMessageBox::Ok)
{
ui->btn1->setDisabled(false);
disconnect(timer,&QTimer::timeout,this,&Widget::on_btn1_clicked);
}else if(btn==QMessageBox::No)
{
}
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QObject>
#include <QTextToSpeech>
#include <QTime>
#include <QTimer>
#include <QDateTime>
#include <QDebug>
#include <QTimerEvent>
#include <QTextEdit>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
signals:
void speech();
private slots:
void on_timer_timeout();
void on_btn1_clicked();
void on_btn2_clicked();
public:
Widget(QWidget *parent = nullptr);
~Widget();
//重写定时器事件处理函数
void timerEvent(QTimerEvent *event);
private:
Ui::Widget *ui;
QTimer *timer;
int Idtime;
};
#endif // WIDGET_H