在本页

TextSelection QML Type

代表连续的文本选区及其属性。更多

Import Statement: import QtQuick
Since: Qt 6.7

属性

方法

详细说明

TextEdit.cursorSelection 表示当前选定(例如通过拖动鼠标选定)的文本范围。它可用于查询和修改所选文本,以及characterblock 格式的属性。

此外,自 6.11 版起,还可以创建显式非可视实例:

TextEdit {
    id: textEdit

    TextSelection {
        id: sel1
    }
}

TextEdit.cursorSelection 和任何显式 TextSelection 实例可用于查询和修改指定范围的文本,以及characterblock 格式的属性。

注意: 该 API 被视为技术预览版,可能会在未来的 Qt 版本中更改或删除。

另请参阅 TextEditQTextCursor

属性文档

alignment : enumeration

包含所选文本的块的对齐方式。

另请参阅 QTextBlockFormat::alignment()。

color : color

选中文本的前景色。

另请参阅 QTextCharFormat::foreground()。

document : TextDocument [since 6.11]

包含所选文本的QQuickTextDocument

此属性在 Qt 6.11 中引入。

另请参阅 QtQuick::TextEdit::textDocument

font : color

所选文本的字体。

另请参阅 QTextCharFormat::font()。

selectionEnd : int [since 6.11]

选区中最后一个字符后的位置。

此属性在 Qt 6.11 中引入。

另请参阅 QtQuick::TextEdit::selectionEnd

selectionStart : int [since 6.11]

选中的第一个字符之前的位置。

该属性在 Qt 6.11 中引入。

另请参阅 QtQuick::TextEdit::selectionStart

text : string

选中的文本,不包含任何富文本标记。

设置此属性后,所选文本将替换为给定的字符串。

方法文档

[since 6.11] void duplicate()

向前复制选中的文本,在副本中保持所有格式不变,并以副本作为选区结束。

例如,如果选中了一整行,该功能会在紧接着的下面插入该行的副本,并选中该副本。如果选中了一个单词,该函数会将其复制到右侧并选中它。

此方法在 Qt 6.11 中引入。

[since 6.11] void linkTo(url destination)

从选定文本创建超链接到destination

此方法在 Qt 6.11 中引入。

另请参阅 QTextCharFormat::setAnchorHref().

[since 6.11] void moveSelectionEnd(MoveOperation op, int n)

根据op 移动selectionEnd n 次,这是moveSelectionStart() 中使用的枚举值之一。

如果moveSelectionStart() 被立即调用,selectionEnd == selectionStart ,则此函数会从这里开始移动,从而选中一系列文本。

此方法在 Qt 6.11 中引入。

另请参阅 QTextCursor::movePosition,QTextCursor::KeepAnchor,moveSelectionStart() 和QTextCursor::MoveOperation

[since 6.11] bool moveSelectionStart(MoveOperation op, int n)

取消选择文本并根据op 移动selectionStart n 次, 是以下枚举值之一:

常量说明
TextSelection.NoMove光标保持原位
TextSelection.Start移动到文档的起点。
TextSelection.StartOfLine移动到当前行的起始位置。
TextSelection.StartOfBlock移至当前块的起始位置
TextSelection.StartOfWord移至当前单词的起始位置。
TextSelection.PreviousBlock移动到上一个块的起始位置
TextSelection.PreviousCharacter移至前一个字符。
TextSelection.PreviousWord移至前一个字的开头。
TextSelection.Up上移一行
TextSelection.Left向左移动一个字符。
TextSelection.WordLeft向左移动一个单词。
TextSelection.End移至文档末尾
TextSelection.EndOfLine移至当前行的末尾。
TextSelection.EndOfWord移至当前单词的末尾
TextSelection.EndOfBlock移至当前块的末尾。
TextSelection.NextBlock移至下一个块的开头。
TextSelection.NextCharacter移至下一个字符。
TextSelection.NextWord移至下一个字。
TextSelection.Down下移一行
TextSelection.Right向右移动一个字符。
TextSelection.WordRight向右移动一个字
TextSelection.NextCell移动到当前表格内下一个表格单元格的开头。如果当前单元格是该行的最后一个单元格,光标将移动到下一行的第一个单元格。
TextSelection.PreviousCell移动到当前表格内上一个表格单元格的起始位置。如果当前单元格是该行的第一个单元格,光标将移动到上一行的最后一个单元格。
TextSelection.NextRow移动到当前表格下一行的第一个新单元格。
TextSelection.PreviousRow移动到当前表格上一行的最后一个单元格。

如果所有操作都成功完成,则返回true ;否则返回false

此方法在 Qt 6.11 中引入。

另请参阅 QTextCursor::movePosition,QTextCursor::MoveAnchor, 和QTextCursor::MoveOperation

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.