Search code examples
qtfocusqmlqtquick2qtquickcontrols2

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


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 ?


Solution

  • This behavior is to be expected, if you follow the chain of events leading to it:

    1. You click on the ComboBox
    2. The ComboBox gains activeFocus
    3. The ComboBox.popup becomes visible
    4. The click is handled, to toggle the visiblity -> the ComboBox becoms invisible.

    Remove the line

    popup.visible: combo.activeFocus
    

    and you are fine.

    Control the visibility via open() and close() instead.