简体   繁体   English

弹出窗口中带有TextField的Qml ComboBox

[英]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 . 我创建了一个自定义ComboBox ,它在页脚中使用带有TextFieldListView ,用于动态地向ComboBox添加选项。 The problem is, that as soon as the popup loses focus (so when the TextField receives focus), the popup gets closed. 问题是,只要弹出窗口失去焦点(因此当TextField获得焦点时),弹出窗口就会关闭。 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). 我试图强制弹出窗口保持打开状态,这确实有效,但是后来阻止TextField获得焦点(我猜是因为弹出窗口一旦调用open()就重新获得焦点)。

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. 我还尝试了PopupclosePolicy属性的所有值,但没有一个帮助。

I am using Qt5.11 . 我使用的是Qt5.11 The forceOpen solution used to work with Qt 5.10, but does not anymore. forceOpen解决方案曾用于Qt 5.10,但现在不再使用了。

Your problem should be fixed if you do not accept the focus on the ComboBox : 如果您不接受对ComboBox的关注,则应修复您的问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM