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. ActiveFocus with TextEdit
Qt 6.11 is out! See what's new in the release blog

ActiveFocus with TextEdit

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 435 Views
  • 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.
  • V Offline
    V Offline
    Valerian
    wrote on last edited by
    #1

    Hi All,

    I have an application which uses QML Treeview. The items of the treeview needs to be editable, hence I have configured a delegate which toggles between text and textedit when user sets a flag.

    Here's the code

    ApplicationWindow {
        id:window
        visible: true
        width: 640
        height: 480
        title: qsTr("File System")
    
        property var clickedIndex: -1
    
        ListModel{
            id:listModel
            ListElement{
                colorName:"red"
            }
            ListElement{
                colorName:"yellow"
            }
            ListElement{
                colorName:"pink"
            }
        }
    
        ListView{
            id:listview
            height: parent.height
            width: 50
            model: listModel
            anchors.left: parent.left
    
            delegate: Component{
                Rectangle{
                    color:  colorName
                    height: 50
                    width: 50
                    MouseArea{
                        anchors.fill: parent
                        onClicked: {
                            console.log("Clicked " + colorName)
                            forceActiveFocus()
                        }
                    }
                }
            }
        }
    
        TreeView {
            id: view
            objectName: "view"
            anchors.margins: 2 * 12
            anchors.left:listview.right
            anchors.top:listview.top
            anchors.right:parent.right
            anchors.bottom:parent.bottom
    
            model: treemodel
            selectionMode:SelectionMode.ExtendedSelection
            headerVisible: true
    
            TableViewColumn {
                title: "Name"
                role: "Title"
                width: 300
                delegate: Component{
                    Item {
                        Text {
                            id: test
                            text: model.Title
                            color: model.IsSelected ? "green" : "blue"
                            width: parent.width
                            visible: !model.EditEnabled
                        }
                        TextEdit{
                            id: textField
                            width: parent.width
                            visible: model.EditEnabled
    
                            onVisibleChanged: {
                                if(visible)
                                {
                                    textField.text = model.Title
                                    forceActiveFocus()
                                }
    
                                onActiveFocusChanged: {
                                    if(!activeFocus)
                                    {
                                        model.EditEnabled = activeFocus
                                    }
                                }
    
                                onTextChanged: {
                                    model.Title = textField.text
                                }
                            }
                        }
                    }
                }
            }
    
            TableViewColumn {
                title: rect
                role:"IsSelected"
                width: 25
    
                delegate: Component{
                    Rectangle{
                        id: rect
                        anchors.right: test.right
                        color: model.IsSelected ? "red" : "teal"
                        MouseArea{
                            anchors.fill: parent
                            onClicked: {
                                model.EditEnabled = true;
                            }
                        }
                    }
                }
            }
        }
    
    }
    

    However, I want the textEdit to loose focus when i click any other item displayed in the screen.

    Please provide me some pointers

    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