6

私は QMLTextEdit要素を持っています。いくつかのテキストを追加し、最後にカーソルを置く予定です。私の方法:

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

しかし、うまくいきません。どうやってやるの?

4

3 に答える 3

6
  1. そうしないと、目に見えない html コードがたくさんあるため、に設定textFormatTextEdit.PlainTextます。
  2. 次のコードは私にとってはうまくいきます。

    Keys.onReturnPressed: {
        event.accepted = true
        txtCommands.text = txtCommands.text + ">: "
        txtCommands.cursorPosition = txtCommands.text.length
    }
    
于 2012-12-25T15:21:53.377 に答える