Pregunta

File: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 }
     }
  }

}

How can I pass the data [1,2,3] into this line property variant items:[1,2,3] from an another page?

Below is how I will be going to use the code in another page:
DropDown.qml{ }

Is there a way by which I can pass the data [1,2,3] inside the above code?

¿Fue útil?

Solución

You can add a setItems() function in the root rectangle of DropDown which would be something like this

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]
}

and in your main.qml or any other qml file you could do this

Rectangle {
    DropDown{
        id: dd
        anchors.fill: parent
    }
    Component.onCompleted: {
        dd.setItems([1, 5, 2, 1, 68, 12])
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top