fix:qtexttospeech crash when initialized

This commit is contained in:
xiaoyifang 2023-05-02 08:24:51 +08:00
parent cfa393f0d9
commit 2f22aa1580
2 changed files with 27 additions and 15 deletions

View file

@ -1,7 +1,6 @@
#include "speechclient.hh"
#include <QtCore>
SpeechClient::SpeechClient( Config::VoiceEngine const & e, QObject * parent ):
QObject( parent ),
internalData( std::make_unique< InternalData >( e ) )
@ -14,28 +13,41 @@ SpeechClient::Engines SpeechClient::availableEngines()
Engines engines;
const auto innerEngines = QTextToSpeech::availableEngines();
for( const auto & engine_name : innerEngines ) {
std::unique_ptr< QTextToSpeech > sp( std::make_unique< QTextToSpeech >( engine_name ) );
for ( const auto & engine_name : innerEngines ) {
const auto sp = new QTextToSpeech( engine_name == "default" ? nullptr : engine_name );
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
if ( !sp || sp->state() == QTextToSpeech::Error )
continue;
#else
if ( !sp || sp->state() == QTextToSpeech::BackendError )
continue;
#endif
qDebug() << engine_name << sp->state();
const QVector< QLocale > locales = sp->availableLocales();
for( const QLocale & locale : locales ) {
for ( const QLocale & locale : locales ) {
//on some platforms ,change the locale will change voices too.
sp->setLocale( locale );
for( const QVoice & voice : sp->availableVoices() ) {
QString name( QString( "%4 - %3 %1 (%2)" )
.arg( QLocale::languageToString( locale.language() ),
( QLocale::countryToString( locale.country() ) ),
voice.name(),
engine_name ) );
SpeechClient::Engine engine( Config::VoiceEngine( engine_name, name, voice.name(), locale, 50, 0 ) );
for ( const QVoice & voice : sp->availableVoices() ) {
const QString name( QString( "%4 - %3 %1 (%2)" )
.arg( QLocale::languageToString( locale.language() ),
( QLocale::countryToString( locale.country() ) ),
voice.name(),
engine_name ) );
Engine engine( Config::VoiceEngine( engine_name, name, voice.name(), QLocale( locale ), 50, 0 ) );
engines.push_back( engine );
}
sp->deleteLater();
}
}
return engines;
}
bool SpeechClient::tell( QString const & text, int volume, int rate )
bool SpeechClient::tell( QString const & text, int volume, int rate ) const
{
if( internalData->sp->state() != QTextToSpeech::Ready )
return false;
@ -47,7 +59,7 @@ bool SpeechClient::tell( QString const & text, int volume, int rate )
return true;
}
bool SpeechClient::tell( QString const & text )
bool SpeechClient::tell( QString const & text ) const
{
return tell(text, internalData->engine.volume, internalData->engine.rate);
}

View file

@ -64,8 +64,8 @@ public:
static Engines availableEngines();
bool tell( QString const & text, int volume, int rate );
bool tell( QString const & text );
bool tell( QString const & text, int volume, int rate ) const;
bool tell( QString const & text ) const;
signals:
void started( bool ok );