TextEdit显示多行可编辑的纯文本或富文本
selectedTextColor
设置选中时文本的颜色
selectionColor
设置选中时的背景颜色
这里把选中时文本颜色设置为green,选中时背景颜色设置为blue
TextEdit{
width: 200
font.pixelSize: 100
text:"hello"
selectedTextColor: "green"
selectionColor: "blue"
}
效果
wrapMode
可参考
这里的wrapMode在使用时和Label下的类似
TextEdit.WordWrap
这里设置TextEdit的width为300并输入文本“Hello,welcome to the QT!”,可以看到,当有空格或者中文的","时假如下一个输入的单词超过给文本编辑框限制的宽度时会自动换行

TextEdit.WrapAnywhere
这里只是修改为TextEdit.WrapAnywhere,和上面不一样的是当一行的内容超出限制时会自动跳到下一行

TextEdit.Wrap
前两种的结合体

滚动功能
Flickable组件是为了能够上下滚动,ensureVisible函数是为了在输入换行时光标矩形始终在可视区域内
Flickable {
id: flick
width: 300; height: 200;
contentWidth: edit.contentWidth
contentHeight: edit.contentHeight //上下滚动的高度等于文本的实际高度
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
width: flick.width
focus: true
wrapMode: TextEdit.Wrap
onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
}
}
1294

被折叠的 条评论
为什么被折叠?



