本页内容

TextSelection QML Type

表示一段连续的文本及其属性。更多...

Import Statement: import QtQuick
Since: Qt 6.7

属性

方法

详细说明

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

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

TextEdit {
    id: textEdit

    TextSelection {
        id: sel1
    }
}

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

注意:此 API 目前处于技术预览阶段,在 Qt 的未来版本中可能会发生变更或被移除。

另请参阅 TextEdit 和QTextCursor 。

属性文档

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 (这是moveSelectionStart() 中使用的枚举值之一)将selectionEnd 移动n 次。

如果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.