【发布时间】:2012-12-06 10:06:30
【问题描述】:
当我单击一个按钮从一个 qml 布局到另一个 qml 时如何加载方法?如果我单击按钮,则有一个editprofile按钮意味着我想显示值,这是我从网络服务获得的,如何做到这一点?任何人都可以发送一些想法。? 谢谢
【问题讨论】:
-
你能更精确一点吗?没有得到你的问题。
标签: qml blackberry-10 blackberry-cascades
当我单击一个按钮从一个 qml 布局到另一个 qml 时如何加载方法?如果我单击按钮,则有一个editprofile按钮意味着我想显示值,这是我从网络服务获得的,如何做到这一点?任何人都可以发送一些想法。? 谢谢
【问题讨论】:
标签: qml blackberry-10 blackberry-cascades
你需要实现这个 onCreationCompleted 方法
onCreationCompleted: {
// call the function, that you need to show first
// first_display()
}
希望这会有所帮助。!!!
【讨论】:
听起来您正试图将从网络服务检索到的数据从一个 QML 视图传递到另一个。导航窗格,您可以在其中使用createObject() 从 .qml 文件构建 UI 屏幕并将新屏幕(页面)推送到导航窗格的视图堆栈上,您的数据可访问新创建的 UI 并可见page 是我认为更具体的描述,也是执行此操作的常用方法。
为了传递您的数据,请在“profileview”QML 页面的根对象(页面、容器)上声明一个属性。然后,在按钮的onClicked() 函数中的第一个QML 文件中,将QML 定义中createObject() 的结果分配给变量。然后,您可以使用此变量将您的数据分配给“profileview”页面上的属性。你的按钮看起来像这样:
// 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
【讨论】:
如果我对您的理解正确,您希望在单击按钮时执行操作。为此,您可以将 onClicked 方法添加到 QML 中的 Button 对象。示例:
Button {
text: "View profile"
onClicked: {
myProfileInfo.visible = true;
}
}
Label {
id: myProfileInfo
text: "This is my profile"
visible: false
}
【讨论】: