7_27homework

该文章展示了一个基于Qt的C++程序,使用QTimerEvent处理定时事件。当用户点击开始按钮,启动两个不同间隔的计时器,一个更新时间显示,另一个检查编辑框文本是否与时间标签匹配。如果匹配,通过QTextToSpeech进行文本语音转换并读出内容。停止按钮则会关闭计时器并重置界面。
摘要由CSDN通过智能技术生成

思维导图:

在这里插入图片描述

代码:
.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->stopBtn->setEnabled(false);
    tId1=startTimer(1000);
   // tId2


    ui->setupUi(this);
}

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


void Widget::on_startBtn_clicked()
{
    tId2=startTimer(500);
    ui->textEdit->setEnabled(false);
    ui->editLabel->setEnabled(false);

}

void Widget::on_stopBtn_clicked()
{
      ui->CrueLabel->setEnabled(true);
      ui->editLabel->setEnabled(true);
      ui->startBtn->setEnabled(true);

    //this->killTimer(tId2);

}
 
void Widget::timeEvent(QTimerEvent *e)
{
    if(e->timerId()==tId1)
    {
        QTime sys_time=QTime::currentTime();
        speecher =new  QTextToSpeech;
        QString t=sys_time.toString("hh:mm::ss");
        ui->CrueLabel->setText(t);
        ui->CrueLabel->setAlignment(Qt::AlignCenter);

    }
     if(e->timerId()==tId2)
     {
         if(ui->editLabel->text()==ui->CrueLabel->text())
         {
            // speecher=new QTextToSpeech(this);
             speecher->say(ui->textEdit->toPlainText());
         }

     }

}






.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QDebug>
#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 timeEvent(QTimerEvent *e);
    QTextToSpeech *speecher;

private slots:
    void on_startBtn_clicked();

    void on_stopBtn_clicked();
signals:

private:
    Ui::Widget *ui;
    int tId1;
    int tId2;
};
#endif // WIDGET_H

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值