需要将数据发送到此文件,不知道如何继续

时间:2014-03-13 09:16:54

标签: qml qt-quick

文件:DropDown.qml

import QtQuick 1.1
Rectangle {
     width:parent.width
     height: parent.height
     color:"transparent"

Rectangle {
    id:comboBox
    property variant items:[1,2,3]

    signal comboClicked;
    width: 141
    height: 30;
    z: 0
    smooth:true;

    Rectangle {
        id:chosenItem
        radius:4;
        width:parent.width;
        height:comboBox.height;
        color: "#454b4d"
        smooth:true;
        Text {
            anchors.top: parent.top;
            anchors.margins: 8;
            id:chosenItemText
            x: 11
            y: 5
            color: "#ffffff"
            text:"Menu";
            anchors.topMargin: 5
            anchors.left: parent.left
            anchors.leftMargin: 12
            font.family: "Arial"
            font.pointSize: 14;
            smooth:true
        }

        MouseArea {
            anchors.bottomMargin: 0
            anchors.fill: parent;
            onClicked: {
                comboBox.state = comboBox.state==="dropDown"?"":"dropDown"
            }
        }
    }

    Rectangle {
        id:dropDown
        width:comboBox.width;
        height:0;
        clip:true;
        radius:4;
        anchors.top: chosenItem.bottom;
        anchors.margins: 2;
        color: "lightblue"

        ListView {
            id:listView
            height:500;
            model: comboBox.items
            currentIndex: 0
            delegate: Item{
                width:comboBox.width;
                height: comboBox.height;


                Text {
                    text: modelData
                    anchors.top: parent.top;
                    anchors.left: parent.left;
                    anchors.margins: 5;

                }
                MouseArea {
                    anchors.fill: parent;
                    onClicked: {
                        comboBox.state = ""
                        chosenItemText.text = modelData;
                        listView.currentIndex = index;
                    }
                }
            }
        }
    }


    states: State {
        name: "dropDown";
        PropertyChanges { target: dropDown; height:30*comboBox.items.length }
    }

    transitions: Transition {
        NumberAnimation { target: dropDown; properties: "height"; easing.type: Easing.OutExpo; duration: 1000 }
     }
  }

}

如何将数据[1,2,3]从另一个页面传递到此行property variant items:[1,2,3]

以下是我将如何在其他页面中使用该代码:
DropDown.qml{ }

有没有办法在上面的代码中传递数据[1,2,3]

1 个答案:

答案 0 :(得分:1)

你可以在DropDown的根矩形中添加一个setItems()函数,就像这样

import QtQuick 1.1
Rectangle {
     width:parent.width
     height: parent.height
     color:"transparent"
     function setItems(items_arg){
         comboBox.items = items_arg
     } 
Rectangle {
    id:comboBox
    property variant items:[1,2,3]
}

并在您的main.qml或任何其他qml文件中,您可以执行此操作

Rectangle {
    DropDown{
        id: dd
        anchors.fill: parent
    }
    Component.onCompleted: {
        dd.setItems([1, 5, 2, 1, 68, 12])
    }
}