1

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

在此处输入图像描述

我使用 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 }
            }
        }
    }

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

我尝试了各种方法,但我无法通过正常工作来达到确切的要求 在此处输入图像描述

4

1 回答 1

1

将 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 = ""
      }
    }
  }
}
于 2012-10-15T06:13:33.940 回答