DJ.Yosemite
DJ.Yosemite

Reputation: 315

Open QML ComboBox drop down menu from the current item position

I have choose an item in combobox. Item's position is 300 for example. If I want to choose new element from combobox . Popup shows from beginning. I want to popup opened from current item position.

 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
            }

Upvotes: 3

Views: 3208

Answers (3)

Cai Cx
Cai Cx

Reputation: 11

For QtQuick.Controls 2, There is a 'popup' property, so we can set the popup position with it's own property 'y', like this:

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

Upvotes: 1

laurapons
laurapons

Reputation: 1103

I was facing the same issue and found out that if you place the popup onOpened, it works perfectly:

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

Upvotes: 0

dtech
dtech

Reputation: 49329

The ComboBox will work the way you want it if the conditions allow for it, that is, if you have enough elements to fill the entire drop down list after the current index item it will show from that item rather than the beginning.

The stock ComboBox however doesn't seem to allow you to specify the drop down list height, so it will take significantly more elements than you have in your example. Or significantly taller elements.

Furthermore, if the current index is the last element, how do you imagine this will show? The list would show only the last element plus a whole bunch of nothing, which is not even possible, the last item cannot move up from the end of the list.

If you really want that behavior, you will have to implement your own combo box element from scratch.

Upvotes: 0

Related Questions