No way to handle comboBox popup visibility



  • Hi all,

    I would like to check the visibility of a QtQuick.Controls2 ComboBox popup but there doesn't seem to be any way to do this.

    I would like the popup to remain visible as long as ComboBox has the activeFocus.
    I tried starting from qt official guide - customizing combobox and by changing the popup closePolicy property to Popup.NoAutoClose, but it does not change anything and popup keeps closing when clicking outside!

    Here is a little example

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.3
    
    Window {
        visible: true
        width: 640
        height: 480
    
        ComboBox {
            id: control
    
            editable: true
            indicator: null
    
            model: ListModel {
                id: model
    
                ListElement { text: "Banana" }
                ListElement { text: "Apple" }
                ListElement { text: "Coconut" }
            }
    
            onActiveFocusChanged: {
                console.log("onActiveFocusChanged", activeFocus)
                if (activeFocus)
                    popup.open()
                else
                    popup.close() // Note: this is not called when clicking outside the ComboBox
            }
    
            popup: Popup {
                y: control.height - 1
                width: control.width
                implicitHeight: contentItem.implicitHeight
                padding: 1
                closePolicy: Popup.NoAutoClose // <---------- completely ignored!
                // visible: control.activeFocus // This should be a declarative way to be used instead of onActiveFocusChanged callback, but doesn't works!
                onVisibleChanged: console.log("visible changed to", visible, ", control activeFocus is", control.activeFocus) // log just to highlight the issue
                contentItem: ListView {
                    clip: true
                    implicitHeight: contentHeight
                    model: control.popup.visible ? control.delegateModel : null
                    currentIndex: control.highlightedIndex
                    ScrollIndicator.vertical: ScrollIndicator { }
                }
    
                background: Rectangle {
                    border.color: "#21be2b"
                    radius: 2
                }
            }
        }
    }
    

    This is the application output:

    when I click on the ComboBox:

    qml: onActiveFocusChanged true
    qml: visible changed to true , control activeFocus is true
    

    when I click outside the Popup:

    qml: visible changed to false , control activeFocus is true
    

    Is this a bug or am I doing something wrong?
    Thanks in advance


  • Moderators

    I played around with it a lot. It feels kind of like a bug to me. It acted weird, like if it disappeared but I moved the window it would reappear. If I clicked outside the window it was fine but if I clicked the window background (i.e. outside the popup but inside the application) it would hide.

    Seems like a bug, but I admit I'm not good with QML so maybe someone else can explain why that is happening. I would file a bug if you don't get an answer.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.