23.07.10

该代码示例展示了如何在Qt环境中创建一个带有图形界面的应用,该应用使用QTimer事件来更新时间显示,并在特定条件下通过QTextToSpeech进行语音播报。用户可以启动和关闭定时器,以及触发语音播报功能。
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    obj_timer = new QTimer(this);
    QPixmap background("D:E:\\QT\\qt\\photo\\b53cc669a88724ab0a47a3eb149d75e.png");  // 替换为你的背景图路径
    QPalette palette;
    palette.setBrush(this->backgroundRole(), QBrush(background));
    this->setPalette(palette);

}

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


void Widget::on_eventBtn_clicked()
{
    ui->eventBtn->setEnabled(false);
    ui->eventBtn_02->setEnabled(true);
    ui->lineEdit->setEnabled(false);



    if(ui->eventBtn->text() == "启动")
    {
        //启动一个定时器
        event_timer = this->startTimer(1000);
        //此时启动了有一个定时器,

    }else
    {
        //关闭一个定时器
        this->killTimer(event_timer);
    }

}


//定时器事件处理函数的定义
void Widget::timerEvent(QTimerEvent *e)
{
    //判断是哪个定时器到位
    if(e->timerId() == event_timer)
    {

        //获取系统日期时间
        QDateTime sys_dt = QDateTime::currentDateTime();

        //将该时间转换为字符串并展示到ui界面上
        ui->label->setText(sys_dt.toString("yyyy-MM-dd hh:mm:ss"));
        time = sys_dt.toString("yyyy-MM-dd hh:mm:ss");
        qDebug()<<time;
        QString settime=ui->lineEdit->text();
        if(settime==time)
        {
            speecher=new QTextToSpeech(this);
            QString msg=ui->textEdit->toPlainText();
                //语音播报文本内容
            speecher->say(msg);
            qDebug()<<msg;

        }

    }
}

void Widget::on_eventBtn_02_clicked()
{
    ui->eventBtn->setEnabled(true);
    ui->eventBtn_02->setEnabled(false);
    ui->lineEdit->setEnabled(true);


   if(ui->eventBtn_02->text() == "关闭")
   {

       this->killTimer(event_timer);
   }

}

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QTimerEvent>
#include <QDateTime>
#include <QtTextToSpeech>
#include <QCoreApplication>


namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);

    void timerEvent(QTimerEvent *e);

    ~Widget();

private slots:

   // void timeout_slot();

    void on_eventBtn_clicked();

    void on_eventBtn_02_clicked();

private:
    Ui::Widget *ui;

    QTimer *obj_timer;

    int event_timer;

    QString time;

    QTextToSpeech *speecher;

};

#endif // WIDGET_H

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值