qtday4

闹钟

qtday4_第1张图片qtday4_第2张图片

 

 

.pro
QT       += core gui texttospeech

.h
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
signals:
    void say();
public slots:
    void say_slot();

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    //声明定时器槽事件
    void timerEvent(QTimerEvent *t)override;


private slots:
    void on_openbtn_clicked();

    void on_closebtn_clicked();


private:
    Ui::Widget *ui;
    //定义一个定时器的标识
    int id;
    //定义两个字符串
    QString time1;
    QString time2;
};

#endif // WIDGET_H
/
.cpp
#include "widget.h"
#include "ui_widget.h"



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

}

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

void Widget::on_openbtn_clicked()
{
    //设置定时器时间
    id=this->startTimer(1000);
    //禁用
    ui->lineEdit->setEnabled(false);
    ui->timeEdit->setEnabled(false);
}

void Widget::on_closebtn_clicked()
{
    //关闭时间
    this->killTimer(id);
    //可用
    ui->lineEdit->setEnabled(true);
    ui->timeEdit->setEnabled(true);
}

void Widget::timerEvent(QTimerEvent *t)
{
    if(t->timerId()==id)
    {
        //获取系统时间
        QDateTime sys_time=QDateTime::currentDateTime();

        //将时间展示到ui界面
        ui->timelabel->setText(sys_time.toString("hh:mm:ss"));
        //字体居中
        ui->timelabel->setAlignment(Qt::AlignCenter);
        time1=ui->timelabel->text();
        time2=ui->timeEdit->text();
        if(time1==time2)
        {
            emit say();
        }
    }
}
void Widget::say_slot()
{
       QTextToSpeech *speaker= new QTextToSpeech(this);
       speaker->say(ui->lineEdit->text());
}

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