QML如何将文本光标放在TextEdit元素的末尾

Bìn*_*yên 5 qt qt4 qml qt-quick

我有一个QML TextEdit元素,我计划附加一些文本并将光标放在最后.我的方法:

import QtQuick 1.1

Rectangle {
    color: "black"
    anchors.fill: parent
    focus: false

    TextEdit {
        id: txtCommands

        color: "lightGreen"
        anchors.fill: parent
        textFormat: TextEdit.RichText
        wrapMode: TextEdit.WordWrap

        font.family: "Consolas"
        font.pixelSize: 15
        focus: true

        MouseArea {
            anchors.fill: parent
            focus: false
        }

        Keys.onPressed: {
            console.log(event.text)

            switch (event.key) {
            case 16777234: // LEFT
            case 16777235: // UP
            case 16777237: // DOWN
            case 16777236: // RIGHT
                event.accepted = true
                break;

            case 16777220:  // Enter
                txtCommands.text = txtCommands.text + ">: "
                txtCommands.selectAll()
                txtCommands.cursorPosition = txtCommands.text.length
                break;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我怎样才能做到这一点?

Ole*_*ber 6

  1. 设置textFormatTextEdit.PlainText因为你有很多不可见的HTML代码.
  2. 以下代码适用于我.

    Keys.onReturnPressed: {
        event.accepted = true
        txtCommands.text = txtCommands.text + ">: "
        txtCommands.cursorPosition = txtCommands.text.length
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 我知道了.在这种情况下尝试`txtCommands.cursorPosition + = 3`. (2认同)