2014-03-13 30 views
0

文件: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

您可以添加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]) 
    } 
} 
+1

夥伴!有效!感謝您的精心答覆! – Jino

+0

不客氣:) –

相關問題