Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Focus and FocusScope
Qt 6.11 is out! See what's new in the release blog

Focus and FocusScope

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 714 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jb2a
    wrote on last edited by
    #1

    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 :)

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved