【问题标题】:QML : Menu Drop-down issueQML:菜单下拉问题
【发布时间】:2012-10-15 14:30:15
【问题描述】:

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

我使用 listview 尝试了以下实现中的要求,但无法得到相同的结果。下面是sn-p

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

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

我尝试了各种方法,但我无法通过正常工作来达到确切的要求

【问题讨论】:

    标签: qt qml qlistview


    【解决方案1】:

    向列表视图的委托添加一个矩形。将该矩形的大小设置为项目的大小(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 = ""
          }
        }
      }
    }
    

    【讨论】:

    • 嗨 Julius,最初我尝试使用相同的方法,但使用上述实现,下拉菜单不起作用。
    • 下拉菜单似乎对我有用。您使用这种方法会遇到什么问题?
    • 我以前不知道这个问题,但是在清理和重建项目时它工作正常。感谢您的快速响应伙伴:)
    猜你喜欢
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 2011-04-13
    • 2016-06-23
    • 2015-11-08
    相关资源
    最近更新 更多