QTday4作业

发布时间:2024年01月11日

思维导图:

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTime>
#include <QTimerEvent>
#include <QPushButton>
#include <QTextToSpeech>
#include <QDebug>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    void timerEvent(QTimerEvent *e);

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    QTextToSpeech *speech;
    int id;
    int secid;
};

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

    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowFlag(Qt::FramelessWindowHint);

    id = startTimer(1000);//启动定时器

    speech = new QTextToSpeech(this);
}

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

void Widget::timerEvent(QTimerEvent *e)
{


    if(e->timerId() == id)
    {
        QTime sys_time = QTime::currentTime();
        ui->syslabel->setText(sys_time.toString("hh:mm:ss"));
    }else if(e->timerId() == secid)
    {
        if(ui->syslabel->text() == ui->lineEdit->text())
        {

            //qDebug() << ui->textlabel->text();
            int i = 0;
            while(i<10)
            {
                speech->say(ui->textlabel->text());
                i++;
            }
            killTimer(secid);
        }
    }
}



void Widget::on_pushButton_clicked()
{
    secid = startTimer(1000);
}

?效果图:

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