Qtday3作业

作业 

Qtday3作业_第1张图片

 头文件

#ifndef WIDGET_H
#define WIDGET_H

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

private slots:

    void on_start_btn_clicked();

    void on_stop_btn_clicked();

private:
    Ui::Widget *ui;
    int tId;  // 闹钟定时器
    QTextToSpeech *Text_Speech; // 语音播报者

};
#endif // WIDGET_H

源文件 

#include "widget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{


    tId = startTimer(1000);   // 启动定时器
    ui->setupUi(this);


    //改变窗口标题
    this->setWindowTitle("闹钟");

    Text_Speech =new QTextToSpeech(this);





}

Widget::~Widget()
{
    delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{

    if(e->timerId() == tId)
    {
        //获取系统时间
        QTime sys_time = QTime ::currentTime();
        //将时间转化成字符串
        QString t = sys_time.toString("hh:mm:ss");

        //将字符串展示到ui界面
        ui->time_lab->setText(t);
        ui->time_lab->setAlignment(Qt::AlignCenter);
        if (ui->time_lab->text() == ui->clockEdit->toPlainText())
        {
            Text_Speech->say(ui->speechEdit->toPlainText());


        }
    }


}


void Widget::on_start_btn_clicked()
{



    //在按下启动按钮后启动按钮为不可用状态
    ui->start_btn->setEnabled(false);

    ui->speechEdit->setEnabled(false);

    ui->clockEdit->setEnabled(false);






}

void Widget::on_stop_btn_clicked()
{
    //在按下停止按钮后启动按钮为可用状态
    ui->start_btn->setEnabled(true);

    ui->speechEdit->setEnabled(true);

    ui->clockEdit->setEnabled(true);


}

你可能感兴趣的:(学习)