QT DAY4作业

1.QT DAY4作业_第1张图片

头代码

#ifndef WIDGET_H
#define WIDGET_H

#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_pushButton_clicked();

private:
    Ui::Widget *ui;
    int id;  //定时器返回id
    int id2;
    QString s;
    QTextToSpeech *speecher;  //创建一个语音播报者

};

#endif // WIDGET_H

源代码

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    id = startTimer(1000);  //每隔1s执行功能代码
    speecher = new QTextToSpeech(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
}

void Widget::timerEvent(QTimerEvent *e)
{
    //判断计时器id
    if(e->timerId() == id)
    {    //获取当前时间
        QTime sys_time = QTime::currentTime();

        s = sys_time.toString("hh:mm:ss");

        //将获取时间放入label中
        ui->sys_lab->setText(s);

        //设置居中显示
        ui->sys_lab->setAlignment(Qt::AlignCenter);
    }else if(e->timerId() == id2)
    {

        if(ui->lineEdit->text() == s)
          {
            for(int i=0;i<5;i++)
            {
               speecher->say(ui->label_2->text());
            }
            //关闭定时器
            killTimer(id2);
            ui->pushButton->setText("启动");
            ui->lineEdit->clear();
          }
    }
}

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


//启动按钮对应槽函数处理
void Widget::on_pushButton_clicked()
{
    if(ui->pushButton->text() == "启动")
    {
        //启动定时器
        id2 = startTimer(1000);

        ui->pushButton->setText("关闭");
    }else {
        //关闭定时器
        killTimer(id2);

        ui->pushButton->setText("启动");
    }
}

效果图

QT DAY4作业_第2张图片

2.思维导图

你可能感兴趣的:(qt,命令模式,开发语言)