Hello Speak Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"mainwindow.h"
#include <QLoggingCategory>
MainWindow::
MainWindow(QWidget *
parent)
:
QMainWindow(parent),
m_speech(0
)
{
ui.setupUi(this
);
QLoggingCategory::
setFilterRules(QStringLiteral("qt.speech.tts=true
\n
qt.speech.tts.*=true"
));
// Populate engine selection list
ui.engine-&
gt;addItem("Default"
, QString("default"
));
foreach (QString engine, QTextToSpeech::
availableEngines())
ui.engine-&
gt;addItem(engine, engine);
ui.engine-&
gt;setCurrentIndex(0
);
engineSelected(0
);
connect(ui.speakButton, &
amp;QPushButton::
clicked, this
, &
amp;MainWindow::
speak);
connect(ui.pitch, &
amp;QSlider::
valueChanged, this
, &
amp;MainWindow::
setPitch);
connect(ui.rate, &
amp;QSlider::
valueChanged, this
, &
amp;MainWindow::
setRate);
connect(ui.volume, &
amp;QSlider::
valueChanged, this
, &
amp;MainWindow::
setVolume);
connect(ui.engine, static_cast
&
lt;void
(QComboBox::
*
)(int
)&
gt;(&
amp;QComboBox::
currentIndexChanged), this
, &
amp;MainWindow::
engineSelected);
}
void
MainWindow::
speak()
{
m_speech-&
gt;say(ui.plainTextEdit-&
gt;toPlainText());
}
void
MainWindow::
stop()
{
m_speech-&
gt;stop();
}
void
MainWindow::
setRate(int
rate)
{
m_speech-&
gt;setRate(rate /
10.0
);
}
void
MainWindow::
setPitch(int
pitch)
{
m_speech-&
gt;setPitch(pitch /
10.0
);
}
void
MainWindow::
setVolume(int
volume)
{
m_speech-&
gt;setVolume(volume /
100.0
);
}
void
MainWindow::
stateChanged(QTextToSpeech::
State state)
{
if
(state ==
QTextToSpeech::
Speaking) {
ui.statusbar-&
gt;showMessage("Speech started..."
);
}
else
if
(state ==
QTextToSpeech::
Ready)
ui.statusbar-&
gt;showMessage("Speech stopped..."
, 2000
);
else
if
(state ==
QTextToSpeech::
Paused)
ui.statusbar-&
gt;showMessage("Speech paused..."
);
else
ui.statusbar-&
gt;showMessage("Speech error!"
);
ui.pauseButton-&
gt;setEnabled(state ==
QTextToSpeech::
Speaking);
ui.resumeButton-&
gt;setEnabled(state ==
QTextToSpeech::
Paused);
ui.stopButton-&
gt;setEnabled(state ==
QTextToSpeech::
Speaking ||
state ==
QTextToSpeech::
Paused);
}
void
MainWindow::
engineSelected(int
index)
{
QString engineName =
ui.engine-&
gt;itemData(index).toString();
delete
m_speech;
if
(engineName ==
"default"
)
m_speech =
new
QTextToSpeech(this
);
else
m_speech =
new
QTextToSpeech(engineName, this
);
disconnect(ui.language, static_cast
&
lt;void
(QComboBox::
*
)(int
)&
gt;(&
amp;QComboBox::
currentIndexChanged), this
, &
amp;MainWindow::
languageSelected);
ui.language-&
gt;clear();
// Populate the languages combobox before connecting its signal.
QVector&
lt;QLocale&
gt; locales =
m_speech-&
gt;availableLocales();
QLocale current =
m_speech-&
gt;locale();
foreach (const
QLocale &
amp;locale, locales) {
QString name(QString("%1 (%2)"
)
.arg(QLocale::
languageToString(locale.language()))
.arg(QLocale::
countryToString(locale.country())));
QVariant localeVariant(locale);
ui.language-&
gt;addItem(name, localeVariant);
if
(locale.name() ==
current.name())
current =
locale;
}
setRate(ui.rate-&
gt;value());
setPitch(ui.pitch-&
gt;value());
setVolume(ui.volume-&
gt;value());
connect(ui.stopButton, &
amp;QPushButton::
clicked, m_speech, &
amp;QTextToSpeech::
stop);
connect(ui.pauseButton, &
amp;QPushButton::
clicked, m_speech, &
amp;QTextToSpeech::
pause);
connect(ui.resumeButton, &
amp;QPushButton::
clicked, m_speech, &
amp;QTextToSpeech::
resume);
connect(m_speech, &
amp;QTextToSpeech::
stateChanged, this
, &
amp;MainWindow::
stateChanged);
connect(m_speech, &
amp;QTextToSpeech::
localeChanged, this
, &
amp;MainWindow::
localeChanged);
connect(ui.language, static_cast
&
lt;void
(QComboBox::
*
)(int
)&
gt;(&
amp;QComboBox::
currentIndexChanged), this
, &
amp;MainWindow::
languageSelected);
localeChanged(current);
}
void
MainWindow::
languageSelected(int
language)
{
QLocale locale =
ui.language-&
gt;itemData(language).toLocale();
m_speech-&
gt;setLocale(locale);
}
void
MainWindow::
voiceSelected(int
index)
{
m_speech-&
gt;setVoice(m_voices.at(index));
}
void
MainWindow::
localeChanged(const
QLocale &
amp;locale)
{
QVariant localeVariant(locale);
ui.language-&
gt;setCurrentIndex(ui.language-&
gt;findData(localeVariant));
disconnect(ui.voice, static_cast
&
lt;void
(QComboBox::
*
)(int
)&
gt;(&
amp;QComboBox::
currentIndexChanged), this
, &
amp;MainWindow::
voiceSelected);
ui.voice-&
gt;clear();
m_voices =
m_speech-&
gt;availableVoices();
QVoice currentVoice =
m_speech-&
gt;voice();
foreach (const
QVoice &
amp;voice, m_voices) {
ui.voice-&
gt;addItem(QString("%1 - %2 - %3"
).arg(voice.name())
.arg(QVoice::
genderName(voice.gender()))
.arg(QVoice::
ageName(voice.age())));
if
(voice.name() ==
currentVoice.name())
ui.voice-&
gt;setCurrentIndex(ui.voice-&
gt;count() -
1
);
}
connect(ui.voice, static_cast
&
lt;void
(QComboBox::
*
)(int
)&
gt;(&
amp;QComboBox::
currentIndexChanged), this
, &
amp;MainWindow::
voiceSelected);
}