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

Action Focus behaviour



  • Hi, it seems that qml actions do not follow the normal focus behaviour.
    In the example attached there are 2 window each with 2 rects that gain focus when clicked. The Keys.onPressed works as expected, meaning that only the item with focus is triggered, whereas the Action shortcut cycles though all four rectangles.

    A snipped for one of the rectangles...

    Keys.onLeftPressed:
                    {
                        if (winN===1)
                            console.log("WIN 1 RECT 2")
                        else
                            console.log("WIN 2 RECT 2")
                    }
                    Action
                    {
                        shortcut: "Ctrl+C"
                        onTriggered:
                        {
                            if (winN===1)
                                console.log("COPY WIN 1 RECT 2")
                            else
                                console.log("COPY WIN 2 RECT 2")
                        }
                    }
    

    When this rectangle is selected it gain focus:
    qml: FOCUS ON FOC 1

    when left is pressed 2 times on Windows 1 the output is:
    qml: WIN 1 RECT 1
    qml: WIN 1 RECT 1
    qml: WIN 1 RECT 1

    However when Ctrl+C is pressed three times:
    qml: COPY WIN 1 RECT 1
    qml: COPY WIN 1 RECT 2
    qml: COPY WIN 2 RECT 1

    The interesting thing is that activeFocus has not changed.
    What I'm missing? Is this the expected behaviour?
    I know a solution would be to have only one action and deliver it to the active element, but this should indeed work in any case... I could not find anything useful in the docs...
    Thanks for help.
    Ivan C.

    FULL CODE OF THE EXAMPLE:
    main.qml

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.Controls 2.15
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        TestWin{
            id:win1
            winN:1
            visible: true
            width: parent.width
            height: parent.height/2
            anchors.top: parent.top
            theColor:"red"
        }
        TestWin{
            id:win2
            winN:2
            visible: false
            width: parent.width
            height: parent.height/2
            anchors.top: parent.top
            theColor:"yellow"
        }
        Text
        {
            anchors.centerIn: mou
            text:"SWITCH WIN"
        }
        MouseArea
        {
            id:mou
            width: parent.width
            height: parent.height/2
            anchors.bottom: parent.bottom
            onClicked:
            {
                win1.visible = !win1.visible
                win2.visible = !win2.visible
            }
        }
    }
    
    

    testWin.qml

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    import QtQuick.Window 2.15
    
    FocusScope {
        id:rect
        property alias theColor: container.color
        property int winN: 0
        Rectangle{
            id:container
            anchors.fill: parent
            FocusScope
            {
                id:foc1
                width:100
                height: 100
                anchors.top: parent.top
                anchors.left: parent.left
                onFocusChanged: if (focus) console.log("FOCUS ON FOC 1")
                Rectangle{
                    id:rect1
                    anchors.fill: parent
                    focus:true
                    color: winN===1 ? " black" : "blue"
                    MouseArea
                    {
                        anchors.fill: parent
                        onClicked: foc1.forceActiveFocus()
                    }
                    Keys.onLeftPressed:
                    {
                        if (winN===1)
                            console.log("WIN 1 RECT 1")
                        else
                            console.log("WIN 2 RECT 1")
                    }
    
    
                    Action
                    {
                        shortcut: "Ctrl+C"
                        onTriggered:
                        {
                            if (winN===1)
                                console.log("COPY WIN 1 RECT 1")
                            else
                                console.log("COPY WIN 2 RECT 1")
                        }
                    }
    
                }
            }
    
            FocusScope
            {
                id:foc2
                width:100
                height: 100
                anchors.bottom: parent.bottom
                anchors.right: parent.right
                onFocusChanged: if (focus) console.log("FOCUS ON FOC 2")
    
                Rectangle{
                    id:rect2
                    anchors.fill: parent
                    focus:true
                    color: winN===1 ? " black" : "blue"
                    MouseArea
                    {
                        anchors.fill: parent
                        onClicked: foc2.forceActiveFocus()
                    }
                    Keys.onLeftPressed:
                    {
                        if (winN===1)
                            console.log("WIN 1 RECT 2")
                        else
                            console.log("WIN 2 RECT 2")
                    }
                    Action
                    {
                        shortcut: "Ctrl+C"
                        onTriggered:
                        {
                            if (winN===1)
                                console.log("COPY WIN 1 RECT 2")
                            else
                                console.log("COPY WIN 2 RECT 2")
                        }
                    }
    
                }
            }
        }
    }