思维导图:
闹钟:
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTimerEvent>
#include<QTimer>
#include<QTime> //时间类
#include<QPushButton> //按钮类头文件
#include<QDebug> //信息调试类,用于打印输出
#include<QTextBrowser>
#include<QMessageBox>
#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_activateBtn_clicked();
void on_stopBtn_clicked();
void my_slot();
void on_timeLab_linkActivated(const QString &link);
private:
Ui::Widget *ui;
int tId;
QPushButton *activateBtn;
QTextToSpeech *speecher;
};
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
tId = startTimer(1000);
//实例化一个语言播报员
speecher = new QTextToSpeech(this);
}
Widget::~Widget()
{
delete ui;
}
//定时器处理函数
void Widget::timerEvent(QTimerEvent *e)
{
//判断是那个定时器到位
if(e->timerId() == tId){
//获取系统时间
QTime sys_time = QTime::currentTime();
//将时间转化为字符串
QString t = sys_time.toString("hh-mm-ss");
ui->timeLab->setText(t);
ui->timeLab->setAlignment(Qt::AlignCenter);
}
}
//启动按钮对应的槽函数
void Widget::on_activateBtn_clicked()
{
while(1){
if(ui->timeLab->text() == ui->lineEdit->text()){
int i = 0;
while(i < 5){
speecher->say(ui->txtLab->toPlainText());
i++;
}
break;
}
}
}
//停止按钮对应的槽函数
void Widget::on_stopBtn_clicked()
{
my_slot();
}
//自定义槽函数
void Widget::my_slot()
{
int ret = QMessageBox::information(this,
"提示",
"您是否确定停止?",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if(ret == QMessageBox::Yes){
ui->activateBtn->setEnabled(false);
ui->txtLab->setEnabled(false);
ui->timeLab->setEnabled(false);
}else{
ui->activateBtn->setEnabled(true);
ui->txtLab->setEnabled(true);
ui->timeLab->setEnabled(true);
}
}