Search code examples
qtcomboboxpopupqmlqcombobox

Qml ComboBox with TextField in Popup


I have created a custom ComboBox that uses a ListView with a TextField in the footer, that's used to dynamically add options to the ComboBox. The problem is, that as soon as the popup loses focus (so when the TextField receives focus), the popup gets closed. I tried to force the popup to stay open, which does work, but then prevents the TextField from receiving focus (I guess because the popup regains focus as soon as open() is called).

ComboBox {
  // ...
  popup: Popup {
    property bool forceOpen: false
    onClosed: {
      if(forceOpen)
        open()
    }

    contentItem: ListView {
      // ...
      footer: TextField {
        onPressed: forceOpen = true
      }
    }
  }
}

I also tried all values for the closePolicy property of the Popup, but none of them helped.

I am using Qt5.11. The forceOpen solution used to work with Qt 5.10, but does not anymore.


Solution

  • Your problem should be fixed if you do not accept the focus on the ComboBox:

    ComboBox {
      focusPolicy: Qt.NoFocus
      popup: Popup {
        // ... 
      }
    }