Frage

How to get different items containers in drop-down feature in QML as shown in figure.

enter image description here

I tried the requirement in below implementation using listview but could not get the same. Below is the snippet

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

With above approach am getting the drop-down as shown in below, which is not according to the requirement.

I tried with various approaches but I was not able to achieve the exact requirement with proper working enter image description here

War es hilfreich?

Lösung

Add a Rectangle to the delegate of the list view. Make the size of that Rectangle to be size of the item (comboBox.width, comboBox.height) minus what ever margin you like and add a radius to that rectangle this should produce the list shown in the first image.

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 = ""
      }
    }
  }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top