DAY3,Qt(完成闹钟的实现,定时器事件处理函数的使用)

1.完成闹钟的实现,到点播报文本框的内容;

---alarm.h---头文件
#ifndef ALARM_H
#define ALARM_H

#include <QWidget>
#include <QTimerEvent>  //定时器处理函数类
#include <QTime>  //时间类
#include <QPushButton>  //按钮类
#include <QLineEdit>  //行编辑类
#include <QLabel>  //标签类
#include <QTextEdit>  //文本编辑类
#include <QDebug>  //输出信息类
#include <QTextToSpeech>

QT_BEGIN_NAMESPACE
namespace Ui { class Alarm; }
QT_END_NAMESPACE

class Alarm : public QWidget
{
    Q_OBJECT

public:
    Alarm(QWidget *parent = nullptr);
    ~Alarm();
    QTextToSpeech *speechptr;  //语音播报类指针

    void timerEvent(QTimerEvent *e);  //要重写的的定时器事件处理函数的声明

private slots:
    void on_startbtn_clicked();
    void on_stopbtn_clicked();

private:
    Ui::Alarm *ui;

    //定义定时器
    int sysid;    //系统时间
    int editid;   //定时时间
};
#endif // ALARM_H
---alarm.cpp---函数实现文件
#include "alarm.h"
#include "ui_alarm.h"

Alarm::Alarm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Alarm)
{
    ui->setupUi(this);
    //实例化一个播报员
    speechptr = new QTextToSpeech(this);
    ui->lineedit->setAlignment(Qt::AlignCenter);  //文本居中对齐

}

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

//启动按钮事件
void Alarm::on_startbtn_clicked()
{
    if(ui->startbtn->text() == "启动")
    {
        //启动报警定时器和系统定时器
        sysid = startTimer(1000);
        editid = startTimer(1000);

        //设置不可使用:自身,行编辑,文本编辑
        ui->startbtn->setEnabled(false);
        ui->lineedit->setEnabled(false);
        ui->textedit->setEnabled(false);

        //设置可用停止按钮
        ui->stopbtn->setEnabled(true);
    }
}

//停止按钮事件
void Alarm::on_stopbtn_clicked()
{
    if(ui->stopbtn->text() == "暂停")
    {
        //暂停报警
        this->killTimer(editid);
        //设置不可使用:自身
        ui->stopbtn->setEnabled(false);

        //设置可用:行编辑,文本编辑,启动按钮
        ui->startbtn->setEnabled(true);
        ui->lineedit->setEnabled(true);
        ui->textedit->setEnabled(true);
    }
}

//定时器事件处理函数
void Alarm::timerEvent(QTimerEvent *e)
{
    //处理系统时间
    if(e->timerId() == sysid)
    {
        QTime systime = QTime::currentTime();
        QString s_systime = systime.toString("hh : mm : ss");  //将获取的时间转为字符串
        ui->timelab->setText(s_systime);  //时间写入标签
        ui->timelab->setAlignment(Qt::AlignCenter);  //剧中对齐
    }else if(e->timerId() == editid)
    {
        QString edittime = ui->lineedit->text();  //自定义时间
        QTime systime = QTime::currentTime();  //系统时间
        QString s_systime = systime.toString("hh : mm : ss");  //将获取的时间转为字符串
        if(s_systime == edittime)
        {
            //实现语音播报文本框内容
            QString content = ui->textedit->toPlainText();
            speechptr->say(content);
        }
    }

}

---main.cpp---测试文件
#include "alarm.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Alarm w;
    w.show();
    return a.exec();
}

点击启动按钮后---

 

 点击暂停按钮后---

2.今日思维导图;

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值