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

how to detect if all child items have focus set to "false" within an item?



  • i have a parent qml called ,

    Rectangle
    {
    id: main
    Item
    {
    id: item1
    }
    Item
    {
    id: item2
    }
    Item
    {
    focus: true
    id: item3
    }
    }

    item1 and item2 contains TextField as child item which receives focus on clicking the item.
    when i launch for first time item3 has focus set to true , but when i click any other item1 or itme2 it looses focus as text field inside item2 or item3 takes the focus.
    now when i close item2 or item3 their children loose focus. at this moment none of the items have focus. so i would want to go set item3 focus to true .
    so basically i want to know when none of the child items in "main" rectangle have focus so that i can set "item3" focus true.


  • Moderators

    Hi @AYYAPPA

    this should work.

    Rectangle
    {
        property bool noActiveFocus: !item1.activeFocus && !item2.activeFocus && !item3.activeFocus
        onNoActiveFocusChanged: if(noActiveFocus) item3.focus = true
    
        id: main
        Item{
             id: item1
        }
    
        Item{
            id: item2
        }
    
        Item{
            focus: true
            id: item3
       }
    }
    

Log in to reply