Qt 如何在QML中限制TextEdit中用户输入的最大行数

Qt 如何在QML中限制TextEdit中用户输入的最大行数,qt,qml,Qt,Qml,我目前正在尝试在矩形中实现文本编辑。问题是,用户仍然能够在矩形范围之外键入内容。我已经将wrapMode设置为TextEdit.Wrap,但问题是TextEdit中的文本可能会从矩形底部溢出。我试图通过使clip为真来解决这个问题,但是用户仍然能够键入字符,但看不到它。我该怎么办 import QtQuick 2.12 import QtQml.Models 2.12 import QtQuick.Controls 2.12 import QtQuick.Controls.Styles 1.4

我目前正在尝试在矩形中实现文本编辑。问题是,用户仍然能够在矩形范围之外键入内容。我已经将wrapMode设置为TextEdit.Wrap,但问题是TextEdit中的文本可能会从矩形底部溢出。我试图通过使clip为真来解决这个问题,但是用户仍然能够键入字符,但看不到它。我该怎么办

import QtQuick 2.12
import QtQml.Models 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.12
import QtGraphicalEffects 1.12
import QtMultimedia 5.0

  Rectangle{

                anchors{
                    top: parent.top
                    topMargin: parent.height/15
                    left: parent.left
                    leftMargin: parent.width/15
                    right: parent.right
                    rightMargin: parent.width/15
                    bottom: parent.bottom
                    bottomMargin: parent.height/1.2
                }
                color: 'white'
                z: 1
                radius: 15
                TextEdit{
                    clip: true
                    cursorPosition: 5
                    anchors.fill: parent
                    wrapMode: TextEdit.Wrap


                }
            }
这是一个矩形文本图像:未设置剪辑,wrapMode:TextEdit.Wrap。这个图像与我想要的正好相反


实际上没有办法限制最大行数。理论上,因为这是开源的,所以您可以做任何事情,但这需要一些努力。我可以建议一种解决方法:

Rectangle {
    id: container
    width: 200
    height: 40
    anchors.centerIn: parent
    color: "orange"
    TextEdit {
        id: txt
        anchors.fill: parent
        padding: 3
        font.pixelSize: 14
        focus: true
        wrapMode: TextEdit.Wrap
        onTextChanged: {
            var pos = txt.positionAt(1, container.height + 1);
            if(txt.length >= pos)
            {
                txt.remove(pos, txt.length);
            }
        }
    }
}

此方法只会切断框外的所有内容。

实际上无法限制最大行数。理论上,因为这是开源的,所以您可以做任何事情,但这需要一些努力。我可以建议一种解决方法:

Rectangle {
    id: container
    width: 200
    height: 40
    anchors.centerIn: parent
    color: "orange"
    TextEdit {
        id: txt
        anchors.fill: parent
        padding: 3
        font.pixelSize: 14
        focus: true
        wrapMode: TextEdit.Wrap
        onTextChanged: {
            var pos = txt.positionAt(1, container.height + 1);
            if(txt.length >= pos)
            {
                txt.remove(pos, txt.length);
            }
        }
    }
}

这种方法只会切断盒子外的所有内容。

Hi!这是一个很好的开始@folibis我只是想知道是否有一种方法可以限制光标从框外退出?我喜欢用户不能在框外书写的方式,但我更喜欢光标停留在框内。您对上面的评论有什么建议吗?很抱歉给您带来了这样的麻烦:(嗨!这是一个很好的开始@folibis我只是想知道是否有办法限制光标离开框外?我喜欢用户不能在框外写字的方式,但我更喜欢光标留在框内。您对上面的评论有什么建议吗?很抱歉给您带来了这样的麻烦:(