Search code examples
qtqmlqtquick2qtquickcontrols2

Move TextArea cursor on MouseArea click in QML


There is a TextArea and i have set activeFocusOnPress porperty to false to block Virtual keyboard pop up, But when user clicks on the TextArea the cursor position should move. To achive this i thought mousearea will be good option. Following is the code:

TextArea{
    id:textArea
    text:"ABCDEFGHIJKLMNOPQRSTUVWXYZ"    
    width:100
    height: 200
    color: "white"
    cursorVisible: true
    activeFocusOnPress: false //To Block virtual Keyboard popup

    background: Rectangle{
        color:"transparent"
        border.color : "white"
        border.width:2
        MouseArea{
            id:mousearea
            anchors.fill:parent
            onClicked: {

                //*** How to set cursor position??                    
            }
        }
    }
}

Solution

  • How about this?

    textArea.cursorPosition = textArea.positionAt(mouseX, mouseY)