QML如何将文本光标放在TextEdit元素的末尾 [英] QML How to put text cursor at the end of TextEdit element

查看:27
本文介绍了QML如何将文本光标放在TextEdit元素的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a QML TextEdit element, I plan for append some text and put the cursor at the end. My method:

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

但它不起作用.我该怎么做?

but it doesn't work. How can i do that?

推荐答案

  1. textFormat 设置为 TextEdit.PlainText,否则会有很多不可见的 html 代码.
  2. 以下代码适用于我.

  1. Set textFormat to TextEdit.PlainText because you have lots of invisible html code otherwise.
  2. The following code works for me.

Keys.onReturnPressed: {
    event.accepted = true
    txtCommands.text = txtCommands.text + ">: "
    txtCommands.cursorPosition = txtCommands.text.length
}

这篇关于QML如何将文本光标放在TextEdit元素的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆