QT-DAY4

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QTextEdit>
#include <QDebug>
#include <QTimer>
#include <QTime>
#include <QTimerEvent>
#include <QDateTime>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT
    QLabel *Lab;
    QLineEdit *linEdit;
    QPushButton *btn1;
    QPushButton *btn2;
    QTextEdit *textEdit;
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    QTextToSpeech *speech;
private slots:
    void btn1_clicked();
    void btn2_clicked();
    void timerEvent(QTimerEvent *event) override;
private:
    Ui::Widget *ui;
    QTimer timer;
    int tid=0;
};
#endif // WIDGET_H

源文件

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{

    speech = new QTextToSpeech(this);
    ui->setupUi(this);
    this->resize(800,500);
    this->setFixedSize(800,500);

    this->Lab=new QLabel(this);
    Lab->resize(380,150);
    Lab->move(20,20);
    Lab->setStyleSheet("background-color:yellow");
    Lab->setStyleSheet("font:56pt");

    this->linEdit=new QLineEdit(this);
    linEdit->resize(350,70);
    linEdit->move(420,20);
    linEdit->setStyleSheet("font:20pt");

    this->btn1=new QPushButton("开始",this);
    btn1->resize(155,70);
    btn1->move(420,100);
    this->btn2=new QPushButton("停止",this);
    btn2->resize(155,70);
    btn2->move(620,100);
    this->textEdit=new QTextEdit(this);
    textEdit->resize(760,240);
    textEdit->move(20,200);
    textEdit->setStyleSheet("font:20pt");

    QObject::connect(btn1,&QPushButton::clicked,this,&Widget::btn1_clicked);
    QObject::connect(btn2,&QPushButton::clicked,this,&Widget::btn2_clicked);

}

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

void Widget::btn1_clicked()
{
    if(this->btn1->text()=="开始")
    {
        tid=this->startTimer(1000);
    }
}
void Widget::btn2_clicked()
{
    if(this->btn2->text()=="停止")
    {
        this->killTimer(tid);
    }
}


void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId()==tid)
    {
        QDateTime sysDateTime=QDateTime::currentDateTime();
        QString dataTime=sysDateTime.toString("hh/mm/ss");
        this->Lab->setText(dataTime);
        if(dataTime==linEdit->text())
        {
            speech->say(textEdit->toPlainText());
        }
    }
}

效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值