Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can't set focus to TextInput placed in contextItem, when ComboBox popup is opened.



  • Hello everyone,
    I have problem with ComboBox customization. I can't set focus to TextInput placed in contextItem, when ComboBox popup is opened. Opened popup disable all events from keyboard, mouse etc. and there is no possibility to write text into TextInput. Here is my code:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.12
    import QtQml 2.3
    ComboBox {
            id: cbx
            Component {
                id: textNotEditable
                Text {
                    text: cbx.currentText
                    horizontalAlignment: Text.AlignLeft
                    verticalAlignment: Text.AlignVCenter
                }
            }
            Component {
                id: textInputEditable
                TextInput {
                    text: "aaa"
                    horizontalAlignment: Text.AlignLeft
                    verticalAlignment: Text.AlignVCenter
                }
            }
            contentItem: Loader {
                sourceComponent: cbx.popup.visible ? textInputEditable : textNotEditable
            }
            model: ListModel {
                id: model
                ListElement { text: "Banana" }
                ListElement { text: "Apple" }
                ListElement { text: "Coconut" }
            }
        }
    
    

    bb5a8697-2691-4efc-bd0d-80ec2c6884a2-image.png

    Do You have any idea how to set focus to TextInput or bridge event filtering when popup is opened?
    Thank You for Your help.
    Vaclav.



  • Hi,

    I tested you code and the problem was you were missing the editable property (https://doc.qt.io/qt-5/qml-qtquick-controls2-combobox.html#editable-prop). With the two components it will work, but I think with one component it is simply, here is the code with a couple of changes:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.12
    import QtQml 2.3
    ComboBox {
    id: cbx
    editable: true
    Component {
    id: textInputEditable
    TextInput {
    readOnly: !cbx.popup.visible
    text: readOnly ? cbx.currentText : "aaa"
    horizontalAlignment: Text.AlignLeft
    verticalAlignment: Text.AlignVCenter
    }
    }
    contentItem: Loader {
    sourceComponent: textInputEditable
    }
    model: ListModel {
    id: model
    ListElement { text: "Banana" }
    ListElement { text: "Apple" }
    ListElement { text: "Coconut" }
    }
    }
    Also I found this https://forum.qt.io/topic/91051/dynamically-override-contentitem-of-a-combobox/4, maybe can help you.

    Regards.


Log in to reply