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

Focus shift across qml files without using focusscope id



  • Hello All,

    There is scenario, where it is required to shift focus across qml files(Components).

    mod1.qml

    import QtQuick 2.0

    Item{

    Row{
    Rectangle{
        id : rect3
        color: activeFocus ? "grey" : "green" ;
        width : 100
        height : 100
        focus: true
        KeyNavigation.right:   rect4
        KeyNavigation.left:   ib1
    
    }
    Rectangle{
        id : rect4
        color: activeFocus ? "grey" : "yellow" ;
        width : 100
        height : 100
        KeyNavigation.left: rect3
        KeyNavigation.right:    ib1
    
    }
    }
    

    }

    mod2.qml

    import QtQuick 2.0

    Item{
    anchors.bottom: parent.bottom
    Column{
    Rectangle{
    id : rect1
    color: activeFocus ? "red" : "grey";
    width : 100
    height : 100
    focus: true
    KeyNavigation.right: rect2
    KeyNavigation.left: vm1

    }
    Rectangle{
        id : rect2
        color: activeFocus ? "blue" : "grey";
        width : 100
        height : 100
    
        KeyNavigation.left:  rect1
        KeyNavigation.right: vm1
    
    }
    }
    

    }

    main.qml

    import QtQuick 2.3
    import QtQuick.Window 2.2

    Window {
    visible: true

    title: qsTr("Hello World")
    
    function focusshift()
    {
    

    //Is it possible to do focus shift between mod1 and mod2 without using focusscope id ?

    }
    Component.onCompleted: {
        focusshift();
    }
    

    }

    Thanks in advance


  • Qt Champions 2017

    Not possible without FocusScope. Qn - What is the problem are you trying to solve ?



  • Problem statement :

    What is the best approach to dynamically create components and assign key navigation focus ?


  • Qt Champions 2017

    If you are using the dynamic component KeyNavigation is complicated to achieve. You can use the FocusScope and achieve the same.


Log in to reply