思维导图
第二题
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QTimerEvent>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void timerEvent(QTimerEvent *e);
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
int id;
QString ti;
int id2;
QTextToSpeech *speecher;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, speecher(new QTextToSpeech(this))
{
ui->setupUi(this);
//启动一个定时器
id = startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
//定时器超时时,自动执行的功能函数
void Widget::timerEvent(QTimerEvent *t)
{
//判断哪个定时器超时
if(t->timerId() == id)
{
//获取系统时间
QTime tim = QTime::currentTime();
//将时间转换成字符串
ti = tim.toString("hh:mm:ss");
//将系统时间放入lab中
ui->label->setText(ti);
//文本居中显示
ui->label->setAlignment(Qt::AlignCenter);
}
else
{
//判断是否到定闹钟的时间
if(ui->lineEdit->text() == ti)
{
for(int i=0;i<5;i++)
{
//读lab中内容
speecher->say(ui->label_3->text());
}
}
}
}
//启动按钮对应的槽函数
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text() == "启动")
{
//启动一个定时器
id2 = startTimer(1000);
//将文本设置“关闭”
ui->pushButton->setText("关闭");
}
else
{
//关闭定时器
killTimer(id2);
//将文本设置“启动”
ui->pushButton->setText("启动");
}
}
标签:Widget,定时器,pushButton,ui,ti,include,QTday4
From: https://blog.csdn.net/chenyangboVb/article/details/141368021