24

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?

Vahid Kharazi
  • 5,723
  • 17
  • 60
  • 103
  • As a reference for future readers, an interesting and topic is the "Dynamic QML Object Creation from JavaScript", see http://qt-project.org/doc/qt-5/qtqml-javascript-dynamicobjectcreation.html – AkiRoss Sep 19 '14 at 17:28

2 Answers2

39

Finally, I used:

Component.onCompleted: {
    setPersonalInfoValue();
}
Vahid Kharazi
  • 5,723
  • 17
  • 60
  • 103
5

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.

Deadron
  • 5,135
  • 1
  • 16
  • 27