Focus and FocusScope



  • Hello,

    I am currently trying to implement some sort of table spreadsheet cell living in a TableView. I don't exactly understand how the focus could be proxy to my cell TextInput when clicking on the cell containing it.

    So I dig into the documentation and find the FocusScope final example. So I first define a clickable widget which is a FocusScope:
    @
    import QtQuick 2.3

    FocusScope {
    id: scope

    property alias text: label.text
    
    //FocusScope needs to bind to visual properties of the children
    property alias color: rectangle.color
    x: rectangle.x
    y: rectangle.y
    width: rectangle.width
    height: rectangle.height
    
    
    Rectangle {
        id: rectangle
        color: "lightsteelblue"
        width: 175
        height: 25
        radius: 10
        antialiasing: true
        border.color: "blue"
    
        TextInput { 
            id: label
            anchors.centerIn: parent
            focus: true
        }
    }
    
    MouseArea { 
        anchors.fill: parent
        onClicked: { scope.focus = true } 
    }
    

    }

    @

    I test two simple example, using firstly a Column as layout for my clickable item:

    @
    import QtQuick 2.3

    Rectangle {
    id: window

    width: 240
    height: 150
    
    Column {
        anchors.fill: parent
    
        Repeater {
            model: 2
    
            MyClickableWidget {
                width: 200
                height: 50
                color: "lightgreen"
                text: index * 42
            }
        }
    }   
    

    }
    @

    All is going all right for this one, you click a rectangle, its internal TextInut got the focus. Almost the same with a ListView (just have to set the focus on listView ot true as it is itself a FocusScope)

    But I encounter a proble when using TableView. Here is my code, using the component defined above:

    @
    import QtQuick 2.3
    import QtQuick.Controls 1.2

    Rectangle {
    id: window

    width: 400
    height: 150
    
    
    ListModel {
        id: model
        ListElement {x: 42; y: 84}
        ListElement {x: 126; y: 168}
    }
    
    TableView {
        anchors.fill: parent
        model: model
        focus: true
    
        itemDelegate: MyClickableWidget {
           color: "lightgreen"
           text: styleData.value
        }
    
        rowDelegate: Rectangle {
            height: 50
        }
    
        TableViewColumn {
            role: "x"
            width: 180
        }
        TableViewColumn {
            role: "y"
            width: 180
        }
    }   
    

    }
    @

    And I really don't understand what is going wrong. I looked at the TableView code (Qt 5.3.1) and see it's a ScrollView < FocusScope, handling a ListView with focus set to true to proxy the focus to its delegate items normally..

    I hope someone has a better view on that problem than me. Thanks in advance :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.