C++DAY49

C++DAY49_第1张图片

widget.h 

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#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 *event);



private slots:
    void on_Btn1_clicked();

    void on_Btn2_clicked();

private:
    Ui::Widget *ui;
    int tId;
    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);

    this->setWindowTitle("闹钟");//设置窗口标题
    this->setWindowIcon(QIcon(":/pic/2561.png"));

    ui->label->setPixmap(QPixmap(":/pic/60.jpg"));
    ui->label->setScaledContents(true);

    ui->lineEdit->setPlaceholderText("设定时间");
    ui->textEdit->setText("少爷,该启动原神了");
    ui->lineEdit->setAlignment(Qt::AlignCenter);
    ui->textEdit->setAlignment(Qt::AlignCenter);
    speecher = new QTextToSpeech(this);
}

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

void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == tId)
    {
        QTime t = QTime::currentTime();
        QString s = t.toString("hh:mm:ss");
        ui->label1->setText(s);
        ui->label1->setAlignment(Qt::AlignCenter);
        if(ui->lineEdit->text() == ui->label1->text())
        {
            int i = 0;
            while(i < 3)
            {
                speecher->say(ui->textEdit->toPlainText());
                i++;
            }

        }
    }
}

void Widget::on_Btn1_clicked()
{
    tId = startTimer(1000);
}

void Widget::on_Btn2_clicked()
{
    this->killTimer(tId);
}

你可能感兴趣的:(c++,开发语言)