Trouble with QtSpeech (again)



  • Hey guys, I recently installed qtspeech (thanks to sgaist), and now I'm having problems again. I start by making a qt widgets application with all the defaults selected, and when I put these simple lines:

     QTextToSpeech voice;
     voice.say("Hello World");
    

    in the constructor for mainwindow.cpp it didn't work. But when I put it in the main.cpp, it worked. I looked in the hello test and all it did, to say something, was this:

    QTextToSpeech * voice;//it even has this code in the mainwindow.cpp, and it doesn't have a problem working.
    voice->say("Hello World");
    

    So I also tried that too, but it didn't work at all, even in the main.cpp. When I try to put that code in the mainwindow.cpp, it comes up with a window saying QtSpeechTest.exe stopped working. Am I doing anything wrong? If so, how could I fix it? Thanks.

    P.S. I've also tried making a speak function that looks like this:

    void MainWindow::saySomething(QString stringToSay){
          QTextToSpeech voice;
          voice.say(stringToSay);
    }
    

    and I made a button that was suppose to call that function and say, "hello world" when I released the button, but that didn't work either.


  • Lifetime Qt Champion

    Hi,

    QTextToSpeech * voice; //<< That's just declaring a pointer
    voice->say("Hello World");
    

    You are not allocating any object of type QTextToSpeech hence the crash.

    void MainWindow::saySomething(QString stringToSay){
          QTextToSpeech voice;
          voice.say(stringToSay);  // << say isn't necessarily synchronous
    }
    

    In this case, voice will get destroyed at the end of the function which might happen before the TTS engine got a change to run hence the silence.



  • Thanks! I changed it like so and now it works!:

        QTextToSpeech * tts = new QTextToSpeech(this);
        tts->say("hello world");
    

  • Lifetime Qt Champion

    If you do that in saySomething then you have a memory leak.

    Make tts a member of your class and be done with it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.