将TextArea光标移动到MouseArea上,单击QML

时间:2017-09-01 05:46:58

标签: qt qml qtquick2 qtquickcontrols2

有一个TextArea,我已将activeFocusOnPress属性设置为false以阻止虚拟键盘弹出,但当用户点击TextArea时,光标位置应移动。为了实现这一点,我认为mousearea将是不错的选择。以下是代码:

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??                    
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这个怎么样?

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