QML:菜单下拉问题

时间:2012-10-15 06:00:29

标签: qt qml qlistview

如何在QML中的下拉功能中获取不同的项容器,如图所示。

enter image description here

我使用listview尝试了以下实现中的要求,但无法获得相同的要求。以下是摘录

import QtQuick 1.1

Rectangle {
    width:400;
    height: 400;

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

            signal comboClicked;
            width: 400;
            height: 30;
            z: 100;
            smooth:true;

            Rectangle {
                id:chosenItem
                radius:4;
                width:parent.width;
                height:comboBox.height;
                color: "#203586"
                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 {
                    width: 400
                    height: 30
                    anchors.bottomMargin: 0
                    anchors.fill: parent;
                    onClicked: {
                        comboBox.state = comboBox.state==="dropDown"?"":"dropDown"
                    }
                }
            }

            Rectangle {
                id:dropDown
                width:comboBox.width/4;
                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 = ""
                            }
                        }
                    }
                }
            }


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

通过上述方法我得到如下所示的下拉列表,这不符合要求。

我尝试了各种方法,但我无法通过正确的工作达到确切的要求 enter image description here

1 个答案:

答案 0 :(得分:1)

将Rectangle添加到列表视图的委托中。将该Rectangle的大小设置为项目的大小(comboBox.width,comboBox.height)减去您喜欢的边距,并为该矩形添加半径,这将产生第一个图像中显示的列表。

delegate: Item{
  width:comboBox.width;
  height: comboBox.height;

  Rectangle {
    width: parent.width - margin;
    height: parent.height - margin;
    anchors.centerIn: parent;

    Text {
      text: modelData
      anchors.top: parent.top;
      anchors.left: parent.left;
      anchors.margins: 5;
    }
    MouseArea {
      anchors.fill: parent;
      onClicked: {
        comboBox.state = ""
      }
    }
  }
}
相关问题