2023.10.20

源代码: 

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

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

    speecher = new QTextToSpeech(this);
    connect(ui->Stop,
            &QPushButton::clicked,
            this,
            &Widget::My_slots_Stop);
    ui->Set_Time->setText("20::58::30");
    ui->Set_Time->setAlignment(Qt::AlignCenter);//居中显示


}

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

void Widget::My_slots_Stop()
{
    speecher->stop();
}

void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId()==Tid){
        QTime sys_time=QTime::currentTime();//获取当前系统时间
        QString s=sys_time.toString("hh::mm::ss");//把系统时间转换成字符串
        ui->Time_Now->setText(s);//写入标签中
        ui->Time_Now->setAlignment(Qt::AlignCenter);//居中显示
        if(s==ui->Set_Time->text())
        {
            while(1)
            {
            speecher->say(ui->Prompt->text());//语音播报
            }
        }
    }
}

//启动闹钟
void Widget::on_Begin_clicked()
{
    Tid=startTimer(1000);
}

//停止闹钟
void Widget::on_Stop_clicked()
{
    emit My_signal_Stop();
}

头文件:

#ifndef WIDGET_H
#define WIDGET_H

#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();

signals:
    void My_signal_Begin();//自定义的信号函数
    void My_signal_Stop();

public slots:
//自定义的槽函数
    void My_slots_Stop();

private slots:
    void on_Begin_clicked();//启动键

    void on_Stop_clicked();//停止键

    void timerEvent(QTimerEvent *e);//时间事件处理函数


private:
    Ui::Widget *ui;

    QTextToSpeech *speecher;
    int Tid;
};
#endif // WIDGET_H

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