Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Expose 'enum QTextToSpeech::State' in QML
QtWS25 Last Chance

Expose 'enum QTextToSpeech::State' in QML

Scheduled Pinned Locked Moved Solved General and Desktop
c++ to qmlenumqtexttospeechstate
4 Posts 2 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _.-rFLo_.-
    wrote on 7 Jul 2017, 17:53 last edited by
    #1

    Hi there
    I am not so much of a QT/C++ buff. Hence, sorry for struggling with the obvious.
    Well, I got stuck trying to react on speech states in QML. Because the emitted enum QTextToSpeech::State is not registered with QML. Please have a look at the snippets below. And most of all, thank you in advance for your hints and helpers!

    in main.cpp I have: 
    
    // load speech module --------------------
    QTextToSpeech* speech = new QTextToSpeech;
    speech->setLocale(QLocale(startLocale));
    
    // load the QML engine --------------------------
    QQmlApplicationEngine engine;
    // making the speech synthesizer class available in QML
    engine.rootContext()->setContextProperty("speech", speech);
    
    then in main.qml
    
    Connections {
                target: speech
                onStateChanged: {
                    console.log("current state: changed!");     // stateChange triggers
                    console.log("the state: "+ speech.state);   // but the state type is not recognised 
                }
            }
    
    
    This produces the following console output: 
    qml: current state: changed!
    QMetaProperty::read: Unable to handle unregistered datatype 'State' for property 'QTextToSpeech::state'
    

    I figured, I must register the QTextToSpeech::state in QML. So I added the following line to main.cpp

    qmlRegisterUncreatableType<QTextToSpeech::State>("QTextToSpeech", 1, 0, "state", "Error: Class uncreatable");
    

    But that gives the following errors, which are too cryptic for me:

    Development/Qt/5.8/clang_64/lib/QtQml.framework/Headers/qqml.h:141: error: no member named 'staticMetaObject' in 'QTextToSpeech::State'
        QML_GETTYPENAMES
        ^~~~~~~~~~~~~~~~
    
    Development/Qt/5.8/clang_64/lib/QtQml.framework/Headers/qqml.h:89: expanded from macro 'QML_GETTYPENAMES'
        const char *className = T::staticMetaObject.className(); \
                                ~~~^
    
    main.cpp:43: in instantiation of function template specialization 'qmlRegisterUncreatableType<QTextToSpeech::State>' requested here
        qmlRegisterUncreatableType<QTextToSpeech::State>("QTextToSpeech", 1, 0, "state", "Error: Class uncreatable");
        ^
    

    And, if there were no errors in C++, I would add an import statement to main.qml

    import QTextToSpeech 1.0
    
    1 Reply Last reply
    0
    • _ Offline
      _ Offline
      _.-rFLo_.-
      wrote on 12 Aug 2017, 15:39 last edited by
      #2

      hi there, this is still open for me.
      asked another way, does anyone know how to use QTextToSpeech states in QML?
      thx a lot

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 13 Aug 2017, 06:50 last edited by
        #3

        Hi,

        IIRC, Q_DECLARE_METATYPE and its friend qRegisterMetaType should be used for that.

        The first one to make the enum known to the meta type system and the second one to use it with signals and slots.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • _ Offline
          _ Offline
          _.-rFLo_.-
          wrote on 14 Aug 2017, 07:48 last edited by
          #4

          Thanks a zillion. For the record:

          qtexttospeech.h already has on line 118 Q_DECLARE_METATYPE(QTextToSpeech::State)

          Hence, what's missing is the qRegisterMetaType part. Which I am adding in main.cpp before the QTextToSpeech module is loaded.

          qRegisterMetaType<QTextToSpeech::State>("State");
          QTextToSpeech* speech = new QTextToSpeech;
          

          Then, in main.qml, I am catching the state changed signals and the state like this:

          Connections {
                  target: speech
          
                  onStateChanged: {
                      console.log("speech.state "+ speech.state);
          
                      if (speech.state == 1){
                          console.log("--- we are speaking! ---");
                      } else if (speech.state == 0){
                          console.log("--- speaking stopped! ---");
                      }
                  }
              }
          
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved