如何将文本字段值从一个qml发送到级联黑莓中的另一个qml页面

时间:2015-07-23 05:07:15

标签: qt blackberry qml blackberry-10 blackberry-cascades

我是BB级联开发的新手。我创建了两个QML Page。我想将数据从一个QML Page传递到另一个。

我想将值{phonenumber(id:phonenumber)和金额(id:amount)从mobile.qml传递给payment.qml

请有人帮助我。提前谢谢。

Mobile.qml:

import bb.cascades 1.4
import bb.data 1.0

Page {
    onCreationCompleted: {
        getData()
        getCircle()
    }

    Container {   
        background: backgroundPaint.imagePaint
        attachedObjects: [
            ImagePaintDefinition {
                id: backgroundPaint
                imageSource: "asset:///images/background.png"
            }
        ]

        TextField {
            id:phonenumber
            hintText: "Enter Phone Number"
            horizontalAlignment: HorizontalAlignment.Center
            verticalAlignment: VerticalAlignment.Center
            topMargin: ui.du(3)

            // On text change the label text is updated.           
            input
            {
                keyLayout: KeyLayout.Text
            }
        }

        TextField {
            id:amount
            hintText: "Enter Amount"
            horizontalAlignment: HorizontalAlignment.Center
            verticalAlignment: VerticalAlignment.Center
            topMargin: ui.du(3)

            // On text change the label text is updated. 
            input
            {
                keyLayout: KeyLayout.Text
            }
        }

        Button {
            id: newButton
            horizontalAlignment: HorizontalAlignment.Center
            verticalAlignment: VerticalAlignment.Center
            topMargin: ui.du(3)
            text: "Recharge"
            appearance: ControlAppearance.Primary
            color: Color.create("#F93249")
            onClicked: {
                var blogpage = goToWebView.createObject();
                navigationPane.push(blogpage);
            }
            attachedObjects: ComponentDefinition {
                id: goToWebView
                source: "payment.qml"
            }         
        }   
    }

    attachedObjects: [
        ComponentDefinition {
            id: newOptionDef
            Option {}
        }
    ] 
}

payment.qml:

import bb.cascades 1.4

Page {
    Container {      
        background: backgroundPaint.imagePaint

        attachedObjects: [
            ImagePaintDefinition {
                id: backgroundPaint
                imageSource: "asset:///images/background.png"
            }
        ]      
    }
}

3 个答案:

答案 0 :(得分:1)

下次请发布与问题相关的代码。至于您的问题,您可以使用parent作为代理来访问另一个项目。 例如,假设我们有一个组件:

<强> Page.qml

import QtQuick 2.4
import QtQuick.Controls 1.2

Item {
    id:page
    width: 200
    height: 200
    property int callee
    function func() {
        txt.text = txt.text + " (changed)"
    }

    Text {
        id: txt
        anchors.centerIn: parent
        text: "click me"
        MouseArea {
            anchors.fill: parent
            onClicked: {
                page.parent.proxyFunction(page.callee);
            }
        }
    }
}

所以Item包含多个Page s:

import QtQuick 2.4
import QtQuick.Window 2.0

Window {
    width: 400
    height: 200

    Row {
        anchors.fill: parent
        function proxyFunction(page) {
            children[page].func();
        }
        Page {callee: 1}
        Page {callee: 0}
    }
}

所以在这里,您可以看到其中一个Text点击Page触发更改另一个Text中的Page

答案 1 :(得分:0)

Page添加以下行:

 import bb.cascades 1.4
 import bb.data 1.0
 Page {

    id: mobilePage // ADD THIS
    property string m_amount // ADD THIS
    property string m_phoneNumber // ADD THIS

    // the rest of the code

    onClicked: { // Buttons onClick
        mobilePage.m_amount = amount.text             // ADD THIS
        mobilePage.m_phoneNumber = phonenumber.text   // ADD THIS
        var blogpage = goToWebView.createObject()
        navigationPane.push(blogpage)
    }
}

现在在payment.qml你可以使用它:

console.log(mobilePage.m_amount)
console.log(mobilePage.m_phoneNumber)

答案 2 :(得分:0)

您必须在页面级别的payment.qml中创建一个属性。

Page{
    Id: payment
    Properly string phonenumber;
    Properly string amount;
    Container{
        Label{
            Id: lblphonenumber
            text: phonenumber 
       } 
       Label{
           Id: lblamout
           text: amount
       } 

}
在你的main.qml中你必须这样做:

onClicked: {
    var blogpage = goToWebView.createObject();
    blogpage.phonenumber = yourvalue;
    blogpage.amount = yourvalue;
    navigationPane.push(blogpage);
}

这就是它:)