01.11

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    void timerEvent(QTimerEvent *e);


private slots:
    void on_startbtn_clicked();

private:
    Ui::Widget *ui;
    QTextToSpeech *speecher;
    int id;
    int flag=0;
};

#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);
    id=startTimer(1000);
    speecher=new QTextToSpeech(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId()==id)
    {
        QTime sys_time=QTime::currentTime();
        QString s=sys_time.toString("hh:mm:ss");
        ui->sys_time_lab->setText(s);
        if(ui->timeedit->text()==s&&flag==1)
        {
            for(int i=0;i<5;i++)
            {
                    speecher->say(ui->text_lab->text());
            }
        }
    }
}

void Widget::on_startbtn_clicked()
{
    if(ui->timeedit->text()=="")
    {
        QMessageBox::information(this,"提示","请输入有效时间",QMessageBox::Ok);
    }
    if(ui->startbtn->text()=="启动")
    {
        flag=1;
        ui->startbtn->setText("关闭");
    }else
    {
        flag=0;
        ui->startbtn->setText("启动");
    }
}

你可能感兴趣的:(命令模式)