Important: Please read the Qt Code of Conduct -

QML: custom ComboBox, close behaviour/focus

  • Hello everyone,

    i'm currently working on a custom combo box control in pure qml (yes, i know there is qtuick controls...), works fine so far, aside from one tiny issue: In virtually any UI-Toolkit Library, a combo box closes as soon as you click anywhere outside the (open) combo box. I can't get that working for me, at least not in an elegant way :(.

    What /should/ work is setting up some fake (invisible) mouse area, which intercepts all clicks and grabs the focus, while the comboBox reacts to onFocusChanged/onActiveFocusChanged or similar and closes its popup. Or i could intercept mouse clicks on the cpp level (basically the same but on a different stage). Anyway, all those solutions look like some dirty workarounds to me :|
    Maybe i haven't read the docs good enough, shouldn't there be some general inbuilt mechanism to lose the focus as soon as the user clicks somewhere else? As this is typical common behaviour to almost any control?


  • Hi,

    What you need is mouse grabbing and checking whether the user has clicked inside or outside the window. AFAIK, the API needed to implement is not available in QML. (Having a screen-wide window is a "no go" for obvious performance and memory reasons)

    I implemented both combo box and menus for Qt Quick Controls, and the core of the interaction is in the QQuickMenuPopupWindow class ( This class also works for menus, meaning it tracks things like moving the mouse cursor on the parent menu window, and such.

    Hope it helps you find some inspiration.



Log in to reply