【问题标题】:Open QML ComboBox drop down menu from the current item position从当前项目位置打开 QML ComboBox 下拉菜单
【发布时间】:2016-10-24 09:20:55
【问题描述】:

我在组合框中选择了一个项目。例如,项目的位置是 300。如果我想从组合框中选择新元素。从一开始就弹出节目。我想从当前项目位置弹出。

 ComboBox {
            id: control
            model: ["First", "Second", "Third","MERHABA","NASILSIN","SELAM","IYIMISIN","DOSTUM","SUAN","BIR","DENEME YAPILIYOR"]
            //width: 350
            //font.pixelSize: 20

            delegate: ItemDelegate {
                width: 350
                text: modelData
                font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal
                font.pixelSize: 30
                highlighted: control.highlightedIndex == index
            }

【问题讨论】:

标签: qt qml qtquickcontrols2


【解决方案1】:

对于 QtQuick.Controls 2,有一个 'popup' 属性,所以我们可以使用它自己的属性 'y' 设置弹出位置,如下所示:

 ComboBox {
        model: ["First", "Second", "Third"]
        delegate: ItemDelegate {
            text: modelData
        }
        popup.y: y + height // popup just below ComboBox
 }

【讨论】:

    【解决方案2】:

    如果条件允许,ComboBox 将按照您希望的方式工作,也就是说,如果您有足够的元素来填充当前索引项目之后的整个下拉列表,它将从该项目而不是开始。

    股票ComboBox 但是似乎不允许您指定下拉列表高度,因此它需要的元素比示例中的要多得多。或者更高的元素。

    此外,如果当前索引是最后一个元素,您认为这将如何显示?该列表将只显示最后一个元素加上一大堆什么都没有,这甚至是不可能的,最后一个项目不能从列表的末尾向上移动。

    如果您真的想要这种行为,则必须从头开始实现自己的组合框元素。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,发现如果您将弹出窗口放在打开状态,它可以完美运行:

      ComboBox {
          id: yearDropdown
          model: yearModel
          onActivated: updateVisibleDate()
      
          popup: Popup {
                id: comboPopup
                clip: true
      
                contentItem: ListView {
                    id: listView
                    implicitHeight: contentHeight
                    model: yearDropdown.popup.visible ? yearDropdown.delegateModel : null
                    onModelChanged: if(model) positionViewAtIndex(yearDropdown.currentIndex, ListView.Center);
                    ScrollIndicator.vertical: ScrollIndicator { }
                }
      
                onOpened: {
                    x = yearDropdown.x  //Set the position you want
                    y = yearDropdown.y + yearDropdown.implicitHeight //Set the position you want
                }
            }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 2018-08-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2018-08-05
        相关资源
        最近更新 更多