Run javascript function when a page loaded in QML

I want to run js function like this

function setPersonalInfoValue(){
        console.debug("setPersonalInfoValue()");
    }

when the page loaded in QML.

I tried this:

Rectangle {
    id: page2
    width: 100
    height: 62

    function setPersonalInfoValue(){
        console.debug("setPersonalInfoValue()");
    }
}

I want to run setPersonalInfoValue() function when I switch between page 1 to page 2.
How can I do this?

Finally, I used:

Component.onCompleted: {
    setPersonalInfoValue();
}

The answer to this question depends on how exactly you are switching pages. If you are dynamically loading the pages then you can use Component.onCompleted. This will execute when QML has finished loading the object. If you are using some other method such as changing the visibility to show a different page then you should rely on the onChanged signal for that property.


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Using optional chaining operator for object property access

Similar Posts