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

View file

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