Cascades NavigationPane:将对象信号连接到另一个qml文件

时间:2013-05-04 09:50:05

标签: c++ qml blackberry-cascades

我使用带有NavigationPane模板的向导创建了一个黑莓应用程序,并在DetailsPage.qml上添加了一些组件(如ButtonTextField)。

我想在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连接按钮,但它有两个问题:

  1. 每次更改屏幕时都会调用connect方法(我只想在启动时连接插槽)。
  2. 当我点击按钮时,我有同样的问题,我没有引用currentPage(但是如果我在那种情况下保存根窗格,使用top()方法,我可以获得对当前的访问权限)页)
  3. 基本上我的问题是: 当我必须将我的插槽连接到信号? 我是怎么做的,如果对象在不同的​​qml文件上?

    我想直接在qml文件中处理C ++上的点击事件。

2 个答案:

答案 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"
    }
 }