我使用带有NavigationPane
模板的向导创建了一个黑莓应用程序,并在 DetailsPage.qml 上添加了一些组件(如 aButton
和 a TextField
)。
我想在 DetailsPage.qml 中的按钮上连接一个插槽功能,当单击该功能时,将文本读入TextArea
同一页面上。
我的问题是,当应用程序启动时,加载的 qml 是 main.qml。如果我尝试获取对 DetailsPage.qml 上的对象的引用,则findChild
返回 0x0,因为它找不到它。main.qml 文件是:
// Navigation pane project template
import bb.cascades 1.0
NavigationPane {
id: navigationPane
Page {
// page with a picture thumbnail
Container {
background: Color.Black
layout: DockLayout {
}
Button {
horizontalAlignment: HorizontalAlignment.Center
verticalAlignment: VerticalAlignment.Center
text: qsTr("New todo")
imageSource: "asset:///images/picture1thumb.png"
onClicked: {
// show detail page when the button is clicked
var page = getSecondPage();
console.debug("pushing detail " + page)
navigationPane.push(page);
}
property Page secondPage
function getSecondPage() {
if (! secondPage) {
secondPage = secondPageDefinition.createObject();
}
return secondPage;
}
attachedObjects: [
ComponentDefinition {
id: secondPageDefinition
objectName: "secondPageDefinition"
source: "DetailsPage.qml"
}
]
}
}
}
onCreationCompleted: {
// this slot is called when declarative scene is created
// write post creation initialization here
console.log("NavigationPane - onCreationCompleted()");
// enable layout to adapt to the device rotation
// don't forget to enable screen rotation in bar-bescriptor.xml (Application->Orientation->Auto-orient)
OrientationSupport.supportedDisplayOrientation = SupportedDisplayOrientation.All;
}
}
detailsPage.qml 文件是:
// Navigation pane project template
import bb.cascades 1.0
Page {
// page with a picture detail
id: pgDetail
objectName: "pgDetail"
actions: [
// create navigation panel actions here
ActionItem {
title: qsTr("Break")
onTriggered: {
imgView.imageSource = "asset:///images/picture1br.png"
}
}
]
paneProperties: NavigationPaneProperties {
backButton: ActionItem {
onTriggered: {
// define what happens when back button is pressed here
// in this case is closed the detail page
navigationPane.pop()
}
}
}
Container {
background: Color.Black
Label {
text: qsTr("Page 2")
horizontalAlignment: HorizontalAlignment.Center
textStyle {
base: SystemDefaults.TextStyles.TitleText
color: Color.Yellow
}
}
ImageView {
id: imgView
imageSource: "asset:///images/picture1.png"
horizontalAlignment: HorizontalAlignment.Center
}
Label {
text: qsTr("Picture description")
horizontalAlignment: HorizontalAlignment.Center
}
TextArea {
objectName: "noteText"
}
Button {
objectName: "insertBtn"
text: qsTr("Insert Note")
}
}
}
我尝试使用信号连接按钮pushTransactionEnded
,但它有两个问题:
- 每次更改屏幕时都会调用 connect 方法(并且我只想在启动时连接插槽)。
- 当我点击按钮时,我有同样的问题,我没有引用 currentPage(但也许如果我在这种情况下保存根窗格,使用 top() 方法,我可以获得对当前页面的访问权限)
所以基本上我的问题是:我什么时候必须将我的插槽连接到信号?如果对象位于不同的 qml 文件中,我该怎么做?
我想不直接在 qml 文件中处理 C++ 上的单击事件。