当我从一个qml布局点击按钮到其他qml时如何加载方法?因为我有一个editprofile按钮,如果我点击按钮意味着我想显示值,我从网络服务获得如何做到这一点?任何人都可以发一些想法。 感谢
答案 0 :(得分:2)
听起来您正试图将从Web服务检索到的数据从一个QML视图传递到另一个QML视图。导航窗格,您可以使用createObject()
从.qml文件构建UI屏幕,并将新屏幕(页面)推送到导航窗格的视图堆栈,新数据的UI可访问您的数据我认为可见页面是一个更具体的描述,也是一种常见的方法。
为了传递您的数据,请在“profileview”QML页面的根对象(Page,Container)上声明一个属性。然后,在按钮的onClicked()
函数的第一个QML文件中,为QML定义分配createObject()
的变量。然后,您可以使用此变量将数据分配到“profileview”页面上的属性。你的Button看起来像这样:
// Assume you have a Navigation Pane called navPane
// You also have to define a component for your QML file
Button {
text: "View profile"
onClicked: {
var profilePage = profileDefinition.createObject();
profilePage.myData = webserviceData;
navPane.push(profilePage);
}
attachedObjects: [
ComponentDefinition {
id: profileDefinition
source: "profilePage.qml"
}
]
}
基于Navigation Pane Cascades示例项目的更详细的完整示例如下:
main.qml
// Navigation pane project template
import bb.cascades 1.0
NavigationPane {
id: navPane
// This property holds and tracks the created profile page
property Page profilePage
/* This property holds some sample webservice data, in practice
* you would load this from the webservice
*/
property variant webserviceData: {
"name": "John Doe",
"email": "john.doe@stackoverflow.com",
"twitter": "@johndoe"
}
Page {
// page with a button to display profile
Container {
layout: DockLayout {
}
Label {
horizontalAlignment: HorizontalAlignment.Center
verticalAlignment: VerticalAlignment.Top
text: webserviceData.name
}
Button {
horizontalAlignment: HorizontalAlignment.Center
verticalAlignment: VerticalAlignment.Center
text: qsTr("Show Profile")
onClicked: {
// show detail page when the button is clicked
profilePage = profileDefinition.createObject();
profilePage.myProfileData = webserviceData;
navPane.push(profilePage);
}
attachedObjects: [
ComponentDefinition {
id: profileDefinition
source: "profilePage.qml"
}
]
}
}
}
onPopTransitionEnded: {
// Clean up any pages that have been popped, to avoid memory leaks
if (profilePage == page) {
page.destroy();
}
}
}
profilePage.qml
// Navigation pane project template
import bb.cascades 1.0
Page {
/* Our data property
* Note: A variant is a QVariant Qt type, so it can easily handle different
* data types, in our case it is a map.
* Without the braces to denote that it is an object you will get a TypeError
* from QML at runtime
*/
property variant myProfileData: { /*empty object*/ }
// page with profile details
paneProperties: NavigationPaneProperties {
backButton: ActionItem {
onTriggered: {
// Pop this page off the stack and go back
navPane.pop();
}
}
}
Container {
Label {
text: qsTr("Profile Page")
horizontalAlignment: HorizontalAlignment.Center
textStyle {
base: SystemDefaults.TextStyles.TitleText
color: Color.Blue
}
}
Label {
text: "Name:"
horizontalAlignment: HorizontalAlignment.Left
}
TextField {
text: myProfileData.name
horizontalAlignment: HorizontalAlignment.Center
}
Label {
text: "Email:"
horizontalAlignment: HorizontalAlignment.Left
}
TextField {
text: myProfileData.email
horizontalAlignment: HorizontalAlignment.Center
}
Label {
text: "Twitter:"
horizontalAlignment: HorizontalAlignment.Left
}
TextField {
text: myProfileData.twitter
horizontalAlignment: HorizontalAlignment.Center
}
}
}
希望这有助于您前进。此外,GitHub上的这些示例项目可能有所帮助:
https://github.com/blackberry/Cascades-Samples/tree/master/quotes
https://github.com/blackberry/Cascades-Samples/tree/master/weatherguesser
答案 1 :(得分:2)
你需要实现这个onCreationCompleted方法
onCreationCompleted: {
// call the function, that you need to show first
// first_display()
}
希望这会有所帮助。!!!
答案 2 :(得分:1)
如果我理解正确,您需要在单击按钮时执行操作。为此,您可以在QML中将onClicked
方法添加到Button
对象中。示例:
Button {
text: "View profile"
onClicked: {
myProfileInfo.visible = true;
}
}
Label {
id: myProfileInfo
text: "This is my profile"
visible: false
}