Я создал приложение Blackberry с помощью мастера с 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?
Я хочу обработать событие clicked на C ++, а не напрямую в файле qml.
Зарегистрируйте объект C ++, содержащий ваш слот, с помощью qml, используя setContextProperty, и вызовите этот слот непосредственно из DetailsPage.qml.
...
Button {
objectName: "insertBtn"text: qsTr("Insert Note")
onClicked:{
cppObject.mySlot()
}
}
...
Вы также можете создать псевдоним как 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"}
}