Strange behavior on ComboBox when playing with focus and popup.visible



  • Hello,

    I have a strange behavior that is annoying me when I try to do this code:

    import QtQuick 2.7
    import QtQuick.Controls 2.0
       
    ApplicationWindow {
        id: window
        visible: true
        width: 640
        height: 480
        
        Rectangle {
            color: "green"
            width: parent.width/2
            height: parent.height
            anchors.right : parent.right
    
            ComboBox {
                id:combo
                popup.visible: combo.activeFocus
                model: [ "Banana", "Apple", "Coconut" ]
            }      
        }
        CheckBox {
            id:check
        }
    }
    

    If I click on the CheckBox and then on theComboBox , theComboBox.popup will appears during 1 ms and then disapears.
    I don't understand why because activeFocus = true

    Do you have any idea why ?



  • @Fheanor Why are you doing it that way in the first place? If you comment out the popup.visible line it behaves as it should.

    At first I thought it first takes the visibility from the mouse area click, then handles the click by itself. Because it's already open when it handles the click it is closed, as it should when it is clicked while it's already open.

    But then I tested. It's more probable that the combobox handles the click ( if you add console.log to MouseArea onClicked it doesn't print), gets active focus, opens the popup which then gets the active focus, combobox looses the active focus and the popup goes not visible.



  • @Eeli-K I don't think it is what you said because if you click on the ComboBox without clicking on the CheckBox, it will works fine. If what you said is true, we should expect same behavior.

    I need to be able to open the popup while clicking on something else than the ComboBox. The MouseArea here is just an example, it could be another Item



  • @Fheanor If you try

    ComboBox {
                id:combo
                popup.visible: combo.activeFocus
                model: [ "Banana", "Apple", "Coconut" ]
                onCurrentIndexChanged: {
                    console.log("new text", combo.currentText)
                    check.forceActiveFocus()
                }
            }
    

    You see that moving the active focus creates the problem somehow.



  • @Eeli-K It is not just about that, I edited my code to make it more simple. And the problem still occur

    I tried this but it doesn't work:

    popup.visible: combo.focus || combo.popup.focus
    


  • @Fheanor said in Strange behavior on ComboBox when playing with focus and popup.visible:

    I need to be able to open the popup while clicking on something else than the ComboBox. The MouseArea here is just an example, it could be another Item

    In that case, how about this:

    ComboBox {
            anchors.top: parent.top
            id:combo
            model: [ "Banana", "Apple", "Coconut" ]
        }
        CheckBox {
            anchors.bottom: parent.bottom
            onCheckedChanged: {
                if (checked) {
                    combo.popup.open()
                }
            }
        }
    


  • @Eeli-K Sorry for the late answer, I was not at work.
    Your solution is what I need, when I play with popup.open() and not popup.visible: combo.activeFocus , it works.
    Thanks a lot !


Log in to reply