Update Label Text with QML method called from C++



  • Hello,

    I wrote a QML function which only updates a label text:

    property alias textchange: label1
    
    function myQmlFunction(msg) {
        item.textchange.text = msg
        console.log(item.textchange.text)
        return
    }
    Label {
            id: label1
            objectName: "labelreceived"
            x: 23
            y: 71
            text: "received"
    }   
    

    When the myQmlFunction is called from an onClicked button event it works. But it doesn't when invoked from C++ method. It reads correctly the text object but can't write. It looks like the function only works when called from a QML code.

    I can assure that there is no problem to invoke the function from C++, it runs and logs at console. The problem is that the text label isn't updated in the screen.

    Is there a issue about updating a QML component property from C++?
    Here is the C++ code:

    QQuickView view;
    view.setSource(QUrl::fromLocalFile("qml/sisc2/main.qml"));
    QObject *object = view.rootObject();
    QMetaObject::invokeMethod(object, "myQmlFunction", Q_ARG(QVariant, "hello"));
    

    Any tip will be very helpful.
    Thanks.


  • Moderators

    Hi @marcosbontempo
    Is this the only code ? I see no problem in above code.



  • Hi @p3c0,
    Thanks for answering. I found out my mistake. I created a new QQuickView component in a class, different from the main viewer. So, when I changed a component property, I was changing a the property of a component from another viewer I've created.

    Sorry for the the dummie issue!


  • Moderators

    @marcosbontempo That's fine.. It happens all the time :)


Log in to reply
 

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