In this Qt6 lesson we want to learn How to Build Text to Speech with Qt6 & C++, so first of all let’s talk about QTextToSpeech module in Qt6.
What is QTextToSpeech in Qt6?
In Qt QTextToSpeech module is a part of the Qt Multimedia module that provides functionality for text-to-speech synthesis. You can use this module to add speech synthesis capabilities to their Qt applications easily. With QTextToSpeech, you can convert text strings into spoken words.
These are some key features and functionalities of QTextToSpeech in Qt:
- Cross-platform Support: QTextToSpeech is designed to work across different platforms supported by Qt, including Windows, macOS, Linux, Android and iOS.
- Multiple Voices and Languages: It supports multiple voices and languages, and ityou can select appropriate voice and language for your application’s target audience.
- Speech Synthesis Options: You can control different aspects of speech synthesis, such as pitch, rate, volume and voice quality.
- Simple API: QTextToSpeech provides a straightforward API for integrating text-to-speech functionality into Qt applications. You can easily initialize the text-to-speech engine, set parameters, and synthesize speech from text.
- Event Handling: It provides signals and slots mechanism for handling events related to speech synthesis, such as completion of speech synthesis or errors during synthesis.
How to Build Text to Speech with Qt6 & C++?
First of all we need to create our design in Qt6,these the widgets that we want to use in this example design.
- Create QMainWindow Application
- You need to add QPalinTextEdit in your window, we are not going to change the object name for that.
- We also need to add four labels and changing their names to Volume, Rate, Pitch and Voices.
- We need three sliders, change the object name to volume, rate and pitch.
- Then we need to add a Qt6 ComboBox and change the object name to voice
- At the end we need to add a Qt6 Button and change the object name to speakButton.
- Also you need to add layout management to your widgets
So this is our design
First you need to open CmakeLists.txt and add these
1 |
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets TextToSpeech) |
Also add that in target_link_libraries
1 |
target_link_libraries(QtTextToSpeechExample PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt6::TextToSpeech) |
Now this is our mainwinodw.h file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include<QTextToSpeech> #include<QVector> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void speak(); void setRate(int rate); void setPitch(int pitch); void setVolume(int volume); void voiceSelected(int index); void localeChanged(const QLocale &locale); private: Ui::MainWindow *ui; QTextToSpeech *m_speech; QVector<QVoice> m_voices; }; #endif // MAINWINDOW_H |
This code defines a MainWindow class in a Qt application with header guards, includes necessary headers, declares the namespace, and declares the MainWindow class, including its constructor, destructor, public slots, and private variables.
This is our mainwindow.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#include "mainwindow.h" #include "./ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); m_speech = new QTextToSpeech(this); // Connect signals to slots connect(ui->speakButton, &QPushButton::clicked, this, &MainWindow::speak); connect(ui->pitch, &QSlider::valueChanged, this, &MainWindow::setPitch); connect(ui->rate, &QSlider::valueChanged, this, &MainWindow::setRate); connect(ui->volume, &QSlider::valueChanged, this, &MainWindow::setVolume); // Connect localeChanged slot to QTextToSpeech's localeChanged signal connect(m_speech, &QTextToSpeech::localeChanged, this, &MainWindow::localeChanged); // Initialize voice combo box localeChanged(QLocale::system()); } MainWindow::~MainWindow() { delete ui; } void MainWindow::speak() { m_speech->say(ui->plainTextEdit->toPlainText()); } void MainWindow::setRate(int rate) { m_speech->setRate(rate/10.0); } void MainWindow::setPitch(int pitch) { m_speech->setPitch(pitch / 10.0); } void MainWindow::setVolume(int volume) { m_speech->setVolume(volume / 100.0); } void MainWindow::voiceSelected(int index) { m_speech->setVoice(m_voices.at(index)); } void MainWindow::localeChanged(const QLocale &locale) { // Convert the QLocale object to a QVariant QVariant localeVariant(locale); // Disconnect the currentIndexChanged signal from the voice combobox to avoid processing while updating disconnect(ui->voice, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected); // Clear the voice combobox to prepare for updating with new voices ui->voice->clear(); // Retrieve the available voices from the QTextToSpeech object m_voices = m_speech->availableVoices(); // Retrieve the current voice being used QVoice currentVoice = m_speech->voice(); // Iterate through the available voices for (const QVoice &voice : qAsConst(m_voices)) { // Add each voice to the voice combobox with its name, gender, and age ui->voice->addItem(QString("%1 - %2 - %3").arg(voice.name()) .arg(QVoice::genderName(voice.gender())) .arg(QVoice::ageName(voice.age()))); // Check if the current voice matches the current iteration voice and set it as selected if (voice.name() == currentVoice.name()) ui->voice->setCurrentIndex(ui->voice->count() - 1); } // Reconnect the currentIndexChanged signal to the voiceSelected slot for future changes connect(ui->voice, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected); } |
This code defines the implementation of the MainWindow class, including its constructor, destructor, and several member functions. In the constructor, it sets up the user interface, initializes a QTextToSpeech object, and connects different signals to slots for handling user interactions. The destructor deletes the user interface object. The member functions handle actions such as speaking text, adjusting speech rate, pitch, and volume, selecting voices, and updating the user interface based on locale changes.
Run the project in Qt6 C++ and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email