QT DAY3

1.思维导图

QT DAY3_第1张图片

2.完成闹钟的实现

头文件

#include 
#include 
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT
    QLineEdit *edit1=new QLineEdit;
   // QTextEdit *edit2=new QTextEdit;

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    void timerEvent(QTimerEvent *e);

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();



private:
    Ui::Widget *ui;
    QTextToSpeech *speecher;

    int tId;
};
#endif // WIDGET_H

源文件

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    speecher =new QTextToSpeech(this);

}

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


void Widget::on_pushButton_clicked()
{
    if(ui->pushButton->text()=="开始")
    {
        tId=startTimer(1000);

    }
}

void Widget::on_pushButton_2_clicked()
{
    if(ui->pushButton_2->text()=="停止")
    {
        this->killTimer(tId);
    }
}

void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId()==tId)
    {
        QTime sys_time=QTime::currentTime();

        QString t=sys_time.toString("hh-mm-ss");

        ui->label_2->setText(t);
        ui->label_2->setAlignment(Qt::AlignCenter);

        QString inputTime=ui->lineEdit->text();
        QTime alarmTime=QTime::fromString(inputTime,"hh-mm-ss");
        ui->lineEdit->setAlignment(Qt::AlignCenter);
        if(alarmTime== sys_time)
        {
            QString speechText=ui->textEdit->toPlainText();
            speecher->say(speechText);
        }

    }

}

 

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