C++Qt day6

#include "dialog.h"
#include "ui_dialog.h"

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

    this->ui->timeLabel->setText(QTime::currentTime().toString("H:mm:ss a"));
    this->startTimer(1000);
    this->ui->timeEdit->setText("请输入定时时间");
    this->ui->chineseEdit->setText("定时时间到了定时时间到了");
    speech = new QTextToSpeech(this);
}

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

void Dialog::timerEvent(QTimerEvent *event)
{
    this->ui->timeLabel->setText(QTime::currentTime().toString("H:mm:ss a"));

    if(event_time == event->timerId())
    {
        speech->say(this->ui->chineseEdit->toPlainText());
        killTimer(event_time);
        this->ui->stopButton->setEnabled(false);
        this->ui->startButton->setEnabled(true);
    }
}


void Dialog::on_startButton_clicked()
{
    int t = this->ui->timeEdit->toPlainText().toInt();
    event_time = this->startTimer(1000 * t);
    this->ui->stopButton->setEnabled(true);
    this->ui->startButton->setEnabled(false);
}

void Dialog::on_stopButton_clicked()
{
    killTimer(event_time);
    this->ui->stopButton->setEnabled(false);
    this->ui->startButton->setEnabled(true);
}
#ifndef DIALOG_H
#define DIALOG_H

#include 
#include 

#include 
#include 
#include 

#include 

QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

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

protected:
    void timerEvent(QTimerEvent *event) override;

private slots:
    void on_startButton_clicked();

    void on_stopButton_clicked();

private:
    Ui::Dialog *ui;

    QTimer *timer;      
    int event_time;    
    QTime alarm;     
    QTextToSpeech *speech;
};

#endif // DIALOG_H

你可能感兴趣的:(c++,ui)