实现闹钟,并播报懒虫...~
daytest.pro:
QT += core gui texttospeechgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += 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.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \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_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic: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 - 幕布