在我的应用程序中,我有一个main.qml,它有一个导航窗格,转到homepage.qml,然后从那里转到profilepage.qml。当我从个人资料页面回到主页时,我需要在主页中触发一个功能。我注意到每当我回弹时,我都会在主页面上调用onPopTransitionEnded。由于主页是从main.qml推送的,因此主页上没有导航窗格,我无法在主页上访问onPopTransitionEnded。以下是我的3个qml视图的示例结构。
NavigationPane {
id: nav
peekEnabled: false
onPopTransitionEnded: {
console.log("POP PAGE from main");
if(page.objectName=="newProfilePage")
{
//I tried to access the function using the homepage id but didnt work
menuScreenPage.reloadView(); // This doesnt work, shows error unknown symbol menuScreenPage
}
page.destroy();
}
Page {
id: mainPage
Container {
//some code
}
onCreationCompleted: {
//Some code and then push to homepage
nav.push(homePageDefenition.createObject());
}
}
}
Page {
id: menuScreenPage
objectName: "menuScreenPage"
function reloadView() //This is the function that is needed to be called on page pop from profile page
{
//some code
}
Container {
//some code
Button { //a button to push to profile page
id:pushButton
horizontalAlignment: HorizontalAlignment.Right
verticalAlignment: VerticalAlignment.Bottom
onClicked: {
console.log("I was clicked!")
nav.push(profilePageDefinition.createObject());
}
}
}
}
Page {
id: newProfilePage
objectName: "newProfilePage"
Container {
//some code
Button { //a button to pop to home page
id:popButton
horizontalAlignment: HorizontalAlignment.Right
verticalAlignment: VerticalAlignment.Bottom
onClicked: {
console.log("I was clicked!")
nav.pop();
}
}
}
}
那么有没有办法可以从main.qml访问homepage.qml的功能?或者是否有任何其他功能,如onPopTransitionEnded,当我从profilepage弹出时,我可以访问homepage.qml本身?请指教。
答案 0 :(得分:3)
似乎您使用此行创建了未命名的对象:
homePageDefenition.createObject()
如果您想稍后访问它,则应将其保存在某些属性中,例如
property var myHomePage: null
...
myHomePage = homePageDefenition.createObject()
nav.push(myHomePage )
...
myHomePage.reloadView();
请记住" menuScreenPage"是本地名称(id),它只在homepage.qml内部工作,没有人可以在该文件之外访问它。
<强> UPD 强>
您甚至可以使用此类代码:
page.reloadView(); // Use local variable "page" instead of internal id "menuScreenPage"