Search code examples
qtfocusqmlqt-quick

Setting activeFocus by clicking anywhere within an Item


I want to set the activeFocus for a FocusScope by clicking anywhere within an Item. Is there a way to achieve this without having a MouseArea over the entire Item? Because it would have to overlay all elements within the Item, making them unclickable.

I'm pretty new to QtQuick/QML and have troubles understanding how to properly implement FocusScopes. I've read about propagating click signals, but couldn't get it to work.

Assuming I have something like this (no FocusScopes for readability):

Rectangle
{
    id: outerRectangle

    width: 1000
    height: 1000

    // various controls in here

    Rectangle
    {
        id: innerRectangle

        anchors.centerIn: parent

        width: 200
        height: 200

        // even more controls in here
    }
}

I want the outerRectangle to get the activeFocus when I click anywhere on the outerRectangle and vice-versa for the innerRectangle. But all controls on both Rectangles still have to work properly. How can I achieve this?


Solution

  • Surround your Item with FocusScope:

    FocusScope {
       Item {
           focus: true
       }
    }
    

    See Qt Doc