头文件
#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());
}
}
}
效果图