增加testtospeech模块
QT += core gui texttospeech
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent>
#include <QTimer>
#include <QTime>
#include <QMouseEvent>
#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 timerEvent(QTimerEvent *e) override;
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
int start_id = 0;
int set_id = 0;
bool flag = 0;
QTextToSpeech *speecher;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, speecher(new QTextToSpeech(this))
{
ui->setupUi(this);
start_id = startTimer(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text() == "Setting")
{
flag = 1;
ui->pushButton->setText("Cancel");
// set_id = startTimer(1000);
}
else
{
flag = 0;
ui->pushButton->setText("Setting");
// killTimer(set_id);
}
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == start_id)
{
//获取系统时间
QTime sysTime = QTime::currentTime();
ui->label_sysTime->setText(sysTime.toString());
//方法1:
if(ui->setTime->text() == sysTime.toString()&&flag)
{
for(int i = 0;i<5;i++)
{
speecher->say(ui->text->text());
}
ui->pushButton->setText("Setting");
flag = 0;
}
}
if(e->timerId() == set_id)
{
//方法2:
//获取系统时间
// QTime sysTime = QTime::currentTime();
// if(ui->setTime->text() == sysTime.toString())
// {
// for(int i = 0;i<5;i++)
// {
// speecher->say(ui->text->text());
// }
//
// ui->pushButton->setText("Setting");
// flag = 1;
// }
}
}
ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QLabel" name="label_sysTime">
<property name="geometry">
<rect>
<x>80</x>
<y>50</y>
<width>281</width>
<height>151</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>34</pointsize>
</font>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>510</x>
<y>130</y>
<width>141</width>
<height>71</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>18</pointsize>
</font>
</property>
<property name="text">
<string>Setting</string>
</property>
</widget>
<widget class="QLabel" name="text">
<property name="geometry">
<rect>
<x>80</x>
<y>270</y>
<width>611</width>
<height>251</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>46</pointsize>
</font>
</property>
<property name="text">
<string>it's time to go</string>
</property>
</widget>
<widget class="QLineEdit" name="setTime">
<property name="geometry">
<rect>
<x>440</x>
<y>50</y>
<width>271</width>
<height>61</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
</font>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="placeholderText">
<string>hh:mm:ss</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>