Qt-workday4

设定一个闹钟,实现定时播报文本框文本功能

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QTimer>   //定时器类头文件
#include<QTime>   //时间类的头文件
#include<QTimerEvent>  //定时器事件处理类
#include<QDateTime>   //日期时间类
#include<QTextToSpeech>
#include<QMessageBox>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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


private slots:

    void timeout_slot(); //自定义处理timeout槽函数

    void alarm_slot();

    void on_startBtn_clicked();

    void on_stopBtn_clicked();

private:
    Ui::Widget *ui;

    QTimer *t1;  //定义一个定时器指针

    QTimer *t2;  //定义一个定时器指针

    QTextToSpeech *speecher;

};

#endif // WIDGET_H

widget.cpp

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

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

    t1 = new QTimer(this);   //给定时器指针实例化空间
    t1->start(0);//不定时

    //将t1的timeout信号绑定到自定义的槽函数中
    connect(t1,&QTimer::timeout,this,&Widget::timeout_slot);

    t2 = new QTimer(this);   //给定时器指针实例化空间
    connect(t2,&QTimer::timeout,this,&Widget::alarm_slot);

    speecher = new QTextToSpeech(this);  //实例化

    ui->stopBtn->setDisabled(true);  //禁用停止按钮
}

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

//对象版本的定时器
void Widget::timeout_slot()
{
    //获取系统当前时间
    QTime sys_time = QTime::currentTime();

    //将QTime转换成字符串
    QString time = sys_time.toString("hh:mm:ss");

    //设置lab内容居中显示
    ui->sys_time_Lab->setAlignment(Qt::AlignCenter);

    //将时间展示到ui界面
    ui->sys_time_Lab->setText(time);
}

void Widget::alarm_slot()
{
    //获取系统当前时间
    QTime sys_time = QTime::currentTime();

    //将QTime转换成字符串
    QString time = sys_time.toString("hh:mm:ss");

    if(time == ui->settime_Edit->text())
    {
         speecher->say(ui->memoEdit->toPlainText());

    }
}


void Widget::on_startBtn_clicked()
{
    if(ui->startBtn->text() == "启动")
    {
        //启动一个定时器
        t2->start(0);//不定时

        QMessageBox::information(this,"提示信息","闹钟启动");

        //开始按钮禁用
        ui->startBtn->setDisabled(true);
        //设定时间文本框按钮禁用
        ui->settime_Edit->setDisabled(true);
        //内容文本框按钮禁用
        ui->memoEdit->setDisabled(true);
        //停止按钮启用
        ui->stopBtn->setEnabled(true);
    }
}

void Widget::on_stopBtn_clicked()
{
    t2->stop();//关闭定时器

    //开始按钮启用
    ui->startBtn->setEnabled(true);
    //设定时间文本框按钮启用
    ui->settime_Edit->setEnabled(true);
    //内容文本框按钮启用
    ui->memoEdit->setEnabled(true);
    //停止按钮禁用
    ui->stopBtn->setDisabled(true);
}

效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值