我使用带有NavigationPane
模板的向导创建了一个黑莓应用程序,并在DetailsPage.qml上添加了一些组件(如Button
和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
连接按钮,但它有两个问题:
基本上我的问题是: 当我必须将我的插槽连接到信号? 我是怎么做的,如果对象在不同的qml文件上?
我想直接在qml文件中处理C ++上的点击事件。
答案 0 :(得分:1)
使用setContextProperty注册包含qml插槽的C ++对象,并直接从DetailsPage.qml调用该插槽
...
Button {
objectName: "insertBtn"
text: qsTr("Insert Note")
onClicked:{
cppObject.mySlot()
}
}
...
答案 1 :(得分:1)
您还可以创建别名为QT
Button {
id:first_button
text: "original text"
onCreationComplete{
Qt.first_qml_fitst_button = first_button
}
}
在第二张表格上你可以
Button {
id:second_button
onClicked:{
Qt.first_qml_fitst_button.text = "i change text"
}
}