TextArea
是一个用于显示和编辑多行可滚动文本的控件,通常用于需要用户输入或展示大量文本的应用中。以下是 TextArea
中常用的属性、信号和方法的详解:
常用属性
-
text:
- 描述:用于设置或获取显示的文本内容。
- 类型:
string
- 示例:
text: "Hello, World!"
-
readOnly:
- 描述:设置为
true
时,文本框为只读状态,用户不能编辑内容。 - 类型:
bool
- 默认值:
false
- 示例:
readOnly: true
- 描述:设置为
-
placeholderText:
- 描述:当
TextArea
为空时显示的占位符文本。 - 类型:
string
- 示例:
placeholderText: qsTr("Enter your text here")
- 描述:当
-
selectByMouse:
- 描述:允许通过鼠标选择文本。
- 类型:
bool
- 默认值:
true
- 示例:
selectByMouse: true
-
wrapMode:
- 描述:控制文本如何换行。
- 类型:
enum
(TextArea.NoWrap
,TextArea.WordWrap
,TextArea.WrapAnywhere
) - 示例:
wrapMode: TextArea.WordWrap
-
padding:
- 描述:内容的内边距。
- 类型:
int
- 示例:
padding: 10
-
implicitWidth 和 implicitHeight:
- 描述:控件的隐式宽度和高度,决定了控件在没有明确设置宽高时的默认尺寸。
- 类型:
int
- 示例:
implicitWidth: 400
,implicitHeight: 300
-
verticalAlignment:
- 描述:文本的垂直对齐方式。
- 类型:
enum
(TextEdit.AlignTop
,TextEdit.AlignBottom
,TextEdit.AlignVCenter
) - 示例:
verticalAlignment: TextEdit.AlignTop
-
background:
- 描述:设置背景属性,如颜色和可见性。
- 类型:
Rectangle
或其他图形元素 - 示例:
background: Rectangle { color: "lightgrey" }
常用信号
-
onTextChanged:
- 描述:当文本内容改变时触发。
- 示例:
TextArea {onTextChanged: console.log("Text changed to: " + text) }
-
onCursorPositionChanged:
- 描述:当光标位置改变时触发。
- 示例:
TextArea {onCursorPositionChanged: console.log("Cursor position: " + cursorPosition) }
常用方法
-
append():
- 描述:在文本的末尾追加文本。
- 示例:
textArea.append("New text")
-
clear():
- 描述:清空文本内容。
- 示例:
textArea.clear()
-
selectAll():
- 描述:选择所有文本。
- 示例:
textArea.selectAll()
-
deselect():
- 描述:取消所有文本的选择。
- 示例:
textArea.deselect()