0111qt

发布时间:2024年01月11日

?

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

?

?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);

   
    给语音播报者实例化空间
    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()=="启动")
    {
        //启动一个定时器
    id=startTimer(1000);

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

    }

}

?0110qt - 幕布

文章来源:https://blog.csdn.net/weixin_57731002/article/details/135538901
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。