王杰qtday4

widget.h

#ifndef WIDGET_H
#define WIDGET_H

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

    void on_stopbtn_clicked();

private:
    Ui::Widget *ui;
    int tid;
    QTextToSpeech *speecher;
    QString s;
};
#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);
    ui->lineEdit->setPlaceholderText("设置闹钟");
    speecher = new QTextToSpeech(this);
}

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

void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == tid)
    {
        //获取时间
        QTime sys_time = QTime::currentTime();
        //转换成字符串
        s = sys_time.toString("hh::mm::ss");
        //放入标签
        ui->timelab->setText(s);
        ui->timelab->setAlignment(Qt::AlignCenter);

        if(ui->lineEdit->text() == s)
        {
            int i = 0;
            ui->textlab->setText("坤坤喊你起床啦,别睡了爱坤");
            while(i<20)
            {
                speecher->say("坤坤喊你起床啦,别睡了爱坤");
                i++;
            }

        }

    }
}

void Widget::on_startbtn_clicked()
{
    if(ui->startbtn->text() == "启动")
    {
        tid = startTimer(1000);
        ui->startbtn->setText("关闭");
    }
    else if(ui->startbtn->text() == "关闭")
    {
        killTimer(tid);
        ui->startbtn->setText("启动");
    }

}


void Widget::on_stopbtn_clicked()
{
    speecher->stop();
}

王杰qtday4_第1张图片王杰qtday4_第2张图片

你可能感兴趣的:(qt)