gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:06:01 25 4
gpt4 key购买 nike

我目前正在尝试在矩形内实现文本编辑。问题是用户仍然可以在矩形范围之外进行输入。我已将 wrapMode 设置为 TextEdit.Wrap,但问题是文本编辑器中的文本可能会从矩形底部溢出。我试图通过使 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);
}
}
}
}

这个方法只是切断了盒子外面的所有东西。

关于qt - 如何在 QML 的 TextEdit 中限制用户输入的最大行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62618930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com