Expose 'enum QTextToSpeech::State' in QML



  • 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
    


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


  • Lifetime Qt Champion

    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.



  • 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! ---");
                }
            }
        }
    

Log in to reply
 

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