0111qt

 实现闹钟,并播报懒虫...~0111qt_第1张图片

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

    }

}

0111qt_第2张图片

 0110qt - 幕布

你可能感兴趣的:(命令模式)