通过单击项目内的任意位置来设置 activeFocus

Setting activeFocus by clicking anywhere within an Item

我想通过单击项目中的任意位置来为 FocusScope 设置 activeFocus。 有没有办法在整个项目上没有 MouseArea 的情况下实现这一目标?因为它必须覆盖 Item 中的所有元素,使它们不可点击。

我是 QtQuick/QML 的新手,很难理解如何正确实施 FocusScopes。我读过有关传播点击信号的内容,但无法让它发挥作用。

假设我有这样的东西(为了可读性没有 FocusScopes):

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
    }
}

我希望在单击 outerRectangle 上的任意位置时 outerRectangle 获得 activeFocus,反之亦然。但是两个 Rectangle 上的所有控件仍然必须正常工作。 我怎样才能做到这一点?

FocusScope 包围你的 Item:

FocusScope {
   Item {
       focus: true
   }
}

Qt Doc