Important: Please read the Qt Code of Conduct -

[Solved] Trigger custom QML handler from c++

  • Hey there,

    maybe it is a really dumb question but i am a bit confused about how signals and slots between c++ and qml work. I like to trigger a Custom event in my QML like following:

    @Text {
    text: applicationData.getCurrentDateTime22()

         Connections {
             target: applicationData
             onImageChanged: console.log("The application data changed!")

    But i am not sure how to trigger "onImageChanged".

    here is some more code:

    @class ApplicationData : public QObject
    Q_INVOKABLE QString getCurrentDateTime22(){
    QString out;
    out = "return value";
    return out;
    public slots:
    //void imageChanged2();

    void imageChanged();

    @int main(int argc, char *argv[])
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    ApplicationData data;
    engine.rootContext()->setContextProperty("applicationData", &data);
    return app.exec();


  • Moderators


    You can't trigger onImageChanged, it's a handler. All you need to do is trigger the signal imageChanged from C++ as
    emit imageChanged()

    and the corresponding handler should respond.

  • Hi,

    thanks for your answer.
    i tried that from inside the getCurrentDateTime22() function. But i didnt get any output in the console. I tried if the console debugging is active and it worked with an native handler. I dont get error message either. I think i miss something.

  • I attached my project as a zip. I dont know if this is common practice here. I hope thats ok. Maybe someone could look inside. I think this is easier than posting snippets. I tried several things now. I am sure i am mising something really stupid, but i cannot find it...

  • Qt Champions 2017

    Your connections are not made when the signal is emitted. While binding the text to string, signal is emitted and by that time no connection exist. Hence you had issue.

    I just tried with this.
    MouseArea {
    anchors.fill: parent
    id : m
    onClicked: {
    lal.text = me.getCurrentDateTime22();

  • Oh yes, that was the missing part.
    now its working and its awsome. Thanks for the help.

Log in to reply