0111qt

 实现闹钟,并播报懒虫...~

 daytest.pro:

QT       += core gui texttospeech

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimerEvent>//定时器事件类
#include <QTime>//时间类
#include <QTextToSpeech>//语音播报类


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)override;//定时器事件函数重写的声明

private slots:
    void on_start_btn_clicked();

private:

    Ui::Widget *ui;
    int id;//定义定时器的id,用来确定是这个id的定时器超时后对应的功能
    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);

        //启动一个定时器
    id=startTimer(1000);
   
    给语音播报者实例化空间
    speecher=new QTextToSpeech(this);

}

Widget::~Widget()
{
    delete ui;
}
//定时器事件函数的实现
void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId()==id)
    {
        //获取当前的系统时间
        QTime sys_time=QTime::currentTime();
        //将系统时间进行类型转换,转换为字符串类型
        QString s=sys_time.toString("hh:mm:ss");
        //将系统时间放入sys_lab中
        ui->sys_lab->setText(s);
        //比较系统时间和闹钟时间是否一致
        if(s==ui->edit->text())
        {
            int n=5;
            while(n)
            {
                speecher->say(ui->say_lab->text());
                n--;
            }
        }

    }

}


void Widget::on_start_btn_clicked()
{

    if(ui->start_btn->text()=="启动")
    {

        ui->start_btn->setText("关闭");
    }
    else
    {
        //关闭定时器
        killTimer(id);
        ui->start_btn->setText("启动");

    }

}

 0110qt - 幕布

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值