在 QML 中加载页面时运行 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15700101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run javascript function when a page loaded in QML
提问by 2 8
I want to run js function like this
我想像这样运行js函数
function setPersonalInfoValue(){
console.debug("setPersonalInfoValue()");
}
when the page loaded in QML.
当页面在 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?
当我在第 1 页和第 2 页之间切换时,我想运行 setPersonalInfoValue() 函数。我该怎么做?
回答by 2 8
Finally, I used:
最后,我使用了:
Component.onCompleted: {
setPersonalInfoValue();
}
回答by Deadron
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.
这个问题的答案取决于您切换页面的准确程度。如果您正在动态加载页面,那么您可以使用 Component.onCompleted。这将在 QML 完成加载对象时执行。如果您正在使用其他方法,例如更改可见性以显示不同的页面,那么您应该依赖该属性的 onChanged 信号。