QML之TextEdit

TextEdit显示一个可编辑的,有格式的文本框。它也可以显示明文和富文本。例如:
TextEdit {
    240
    text: "<b>Hello</b> <i>World!</i>"
    font.family: "Helvetica"
    font.pointSize: 20
    color: "blue"
    focus: true
}

设置focus为真 来使得TextEdit接受键盘焦点。
注意:文本编辑框没有实现滚动操作,光标操作,以及其他的一些针对特定于感官等的行为。例如添加flickable 滚动去跟随光标:
Flickable {
id: flick

300; height: 200;
contentWidth: edit.paintedWidth
contentHeight: edit.paintedHeight
clip: true

function ensureVisible(r)
{
if (contentX >= r.x)
contentX = r.x;
else if (contentX+width <= r.x+r.width)
contentX = r.x+r.width-width;
if (contentY >= r.y)
contentY = r.y;
else if (contentY+height <= r.y+r.height)
contentY = r.y+r.height-height;
}

TextEdit {
id: edit
flick.width
height: flick.height
focus: true
wrapMode: TextEdit.Wrap
onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
}
}
一个特别的感官可能使用平滑的滚动(例如使用SmoothedAnimation),可能又一个可以看得见的滚动条,或者是一个淡出到显示位置的滚动条,等等。
剪贴板支持 可以提供cut(),copy(),paste()函数,selection可以依靠设置selectByMouse去在传统的鼠标机制中处理各类信息。通过设置起始位置与终止位置来实现完整的处理,或者用selectAll()或者selectWord();
你可以通过positionAt()和positionToREctangle()在光标位置和像素点之间进行转换。
具体详见 Text and TextInput.;

属性文档:
activeFocusOnPress : bool
TextEdit是否应该在鼠标下压时添加激活焦点事件。默认该设置为真。

baseUrl : url
这个属性制定了一个文本text中基本的被用来解决相关textURL。
默认值是QML实例化的TextEdit项目的url。

canPaste : bool
如果TextEdit是可写的并且剪贴板的内容是合适的去张贴到TextEdit中时,返回真。

canRedo : bool
如果TextEdit使可以编辑的并且这里确实可以允许重做操作时,返回真值。

canUndo : bool
如果TextEdit是可以编辑的并且拥有钱一个操作去允许被撤消操作时,返回真值。

color : color
文本颜色。
// green text using hexadecimal notation
TextEdit { color: "#00FF00" }
// steelblue text using SVG color name
TextEdit { color: "steelblue" }

contentHeight : real
返回文本的高度,包括那些因为文本没有适合设置的高度而被覆盖了的高度值。

contentWidth : real
返回文本的宽度,包括那些过去由于不充分环绕,且wrapMode已经被设置好的,而覆盖的高度。

cursorDelegate : Component
编辑文本框中的光标代理。
如果为一个文本编辑框设置了cursorDelegate,这个代理将被用作绘制一个新光标来替代标准的光标。当需要一个光标时,委托的一个实例将被创建并且通过文本编辑器来进行管理,该代理的属性X和Y将被设置并作为当前字符左上角的一个像素。
注意:代理组件的根项目
一定是一个QQuickItem 或者 QQuickItem 设备项目。

cursorPosition : int
光标文本编辑的位置

cursorRectangle
rectangle





生活的残酷,让我们习惯了忘记疲倦,一直奔向远方,追寻着自己的梦想。
原文地址:https://www.cnblogs.com/L-Arikes/p/4332001.html