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; //给定时器指针实例化空间
//将定时器的timeout信号连接到自定义的槽函数中
connect(timer, &QTimer::timeout,
this, &Widget::on_timer_timeout);
//启动定时器
timer->start(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_startBtn_clicked()
{
ui->startBtn->setEnabled(false);
ui->exitBtn->setEnabled(true);
ui->lineEdit->setEnabled(false);
ui->tishi->setEnabled(false);
}
void Widget::on_exitBtn_clicked()
{
//关闭定时器
// timer->stop();
ui->startBtn->setEnabled(true);
ui->exitBtn->setEnabled(false);
ui->lineEdit->setEnabled(true);
ui->tishi->setEnabled(true);
}
void Widget::on_timer_timeout()
{
//将系统时间展示到ui界面
//1、获取系统日期
QDateTime sysDateTime = QDateTime::currentDateTime();
//2、将日期事件转换为字符串
QString dateTime = sysDateTime.toString("hh:mm:ss");
//3、将字符串展示到ui界面中
ui->systemTimelab->setText(dateTime);
if(dateTime == ui->lineEdit->text())
{
ui->tishi->setText("就很nice");
//将文本信息读取出来
QTextToSpeech *speech = new QTextToSpeech(this);
QString text = ui->tishi->toPlainText();
speech->say(text);
}
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTimer> //定时器类
#include<QTime> //时间类
#include<QTimerEvent> //定时器事件类
#include<QDateTime> //日期类
#include <QLabel>
#include <QLineEdit>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
//系统定义的关于启动按钮对应的槽函数
void on_startBtn_clicked();
//系统定义的关于退出按钮对应的槽函数
void on_exitBtn_clicked();
//自定义的关于timeout的槽函数
void on_timer_timeout();
private:
Ui::Widget *ui;
QTimer *timer; //定时器指针
};
#endif // WIDGET_H