代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::test);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::test(){
QString str="欢迎您";
this->ui->lineEdit->setText(str);
QTextToSpeech *Speecher=new QTextToSpeech;
const QVector<QVoice> voices = Speecher->availableVoices();
for (QVoice voice : qAsConst(voices)) {
if(voice.gender() == QVoice::Female)
{
Speecher->setVoice(voice);//选择女生
break;
}
}
Speecher->setRate(0);//调节阅读速度,取值范围为-1.0到1.0
Speecher->setVolume(1.0);// 调节阅读声音大小,取值范围为0.0到1.0
Speecher->say(str);
}
ui设计: