QTextEdit中的"selection"和"cursor"有什么区别?
What is the difference between the "selection" and the "cursor" in QTextEdit?
在 QTextEdit 的文档中,我看到定义了两个信号:
void cursorPositionChanged()
void selectionChanged()
前者的文档只说 "This signal is emitted whenever the position of the cursor changed." 后者的文档只说 "This signal is emitted whenever the selection changes." 在 QTextEdit 文档的其他地方,听起来这两个概念是相同的;例如,"Selection of text is handled by the QTextCursor class, which provides functionality for creating selections, retrieving the text contents or deleting selections." 那么这两个信号有什么区别??
我认为解释差异的最好方法是展示一些最小但完整的示例。首先,假设我们有一些 textEdit,并将其连接到提到的信号:
connect(ui->textEdit, &QTextEdit::selectionChanged, this, []
{
qDebug() << "Selection Changed";
});
connect(ui->textEdit, &QTextEdit::cursorPositionChanged, this, []
{
qDebug() << "Cursor Position Changed";
});
现在可能有以下用例会导致信号被触发,因此输出会因此而有所不同:
- 您无需 select 即可通过键盘输入一些文本。在这种情况下,只有光标位置会发生变化,因此 &QTextEdit::cursorPositionChanged 将被触发,这将导致打印 "Cursor Position Changed" 文本。
- 您可以在 Windows 到 select 所有文本上使用 Ctrl+A 组合键。在这种情况下 &QTextEdit::selectionChanged 将被触发,但 &QTextEdit::cursorPositionChanged 不会。
- 在 Windows 上,您可以单击鼠标左键,然后移动鼠标左键,这样文本就不会被实际 select 编辑。在这种情况下,您会多次触发 &QTextEdit::selectionChanged。不确定为什么(也许是 Qt 问题?)
换句话说,我认为你应该试试这个例子,看看如何检索不同的输出,以便看到和感受差异。最后,这两个信号之间的主要区别在于,一个是关于文本光标位置在 QTextEdit 元素中移动,另一个是关于通过键盘、鼠标交互改变 selected 文本。此外,这两个信号都可能被触发,因为两个光标位置都可能发生变化,select编辑文本也是如此。
一个QTextCursor has two positions relevant to these signals, anchor and position.
selectionChanged
将在锚点或位置更改时发出 1.
cursorPositionChanged
位置改变时会发出。
QTextCursor
is modeled on the way a text cursor behaves in a text editor, providing a programmatic means of performing standard actions through the user interface. A document can be thought of as a single string of characters. The cursor's current position()
then is always either between two consecutive characters in the string, or else before the very first character or after the very last character in the string.
A QTextCursor
also has an anchor()
position. The text that is between the anchor()
and the position()
is the selection. If anchor() == position()
there is no selection.
- 除非“无选择”改变位置,即改变前后相等
游标是一个迭代器:这是您可以 "point to" 文档中某些内容的唯一方式。因此,要创建一个选择,您需要两个光标位置 - anchor()
位置和当前 position()
。这就是游标同时具有它们的原因(它不是最干净的 API,但可行的选择 API 会复制很多游标的 API)。由于光标用于指向文档,因此选择取决于它们,并且您无法在没有光标的情况下定义选择。为清楚表明游标是构建选择的基础,您只能将选择创建为游标的 属性。
由于游标是迭代器,因此一个文档上可以有多个游标。 cursorPositionChanged()
仅在主光标上发出,代表可见的闪烁光标。因此,此信号仅在 QTextEdit
上可用,它维护自己的主游标。文档本身没有那个信号,因为没有一个光标是特殊的。因此你有 QTextDocument::cursorPositionChanged(const QTextCursor &)
信号。
同样,一个文档可以有多个选择 - 一个选择是一个 属性 游标,但只有主游标的选择可见,并且 selectionChanged()
仅在主游标时发出选择改变。可以存在其他选择,因为选择只是文档中的一个范围,使用不一定要可见的选择有很多原因。
您可以在没有 QTextEdit
的情况下以编程方式对文档进行所有编辑,因此 QTextDocument
甚至没有 selectionChanged
信号,因为没有单一的选择赞成。不过,当光标的选择发生变化时,它会发出光标变化信号。
要理解这些概念,仅查看 QTextDocument
并了解其 API 与 QTextEdit
的 API 之间的差异会有所帮助。
在 QTextEdit 的文档中,我看到定义了两个信号:
void cursorPositionChanged()
void selectionChanged()
前者的文档只说 "This signal is emitted whenever the position of the cursor changed." 后者的文档只说 "This signal is emitted whenever the selection changes." 在 QTextEdit 文档的其他地方,听起来这两个概念是相同的;例如,"Selection of text is handled by the QTextCursor class, which provides functionality for creating selections, retrieving the text contents or deleting selections." 那么这两个信号有什么区别??
我认为解释差异的最好方法是展示一些最小但完整的示例。首先,假设我们有一些 textEdit,并将其连接到提到的信号:
connect(ui->textEdit, &QTextEdit::selectionChanged, this, []
{
qDebug() << "Selection Changed";
});
connect(ui->textEdit, &QTextEdit::cursorPositionChanged, this, []
{
qDebug() << "Cursor Position Changed";
});
现在可能有以下用例会导致信号被触发,因此输出会因此而有所不同:
- 您无需 select 即可通过键盘输入一些文本。在这种情况下,只有光标位置会发生变化,因此 &QTextEdit::cursorPositionChanged 将被触发,这将导致打印 "Cursor Position Changed" 文本。
- 您可以在 Windows 到 select 所有文本上使用 Ctrl+A 组合键。在这种情况下 &QTextEdit::selectionChanged 将被触发,但 &QTextEdit::cursorPositionChanged 不会。
- 在 Windows 上,您可以单击鼠标左键,然后移动鼠标左键,这样文本就不会被实际 select 编辑。在这种情况下,您会多次触发 &QTextEdit::selectionChanged。不确定为什么(也许是 Qt 问题?)
换句话说,我认为你应该试试这个例子,看看如何检索不同的输出,以便看到和感受差异。最后,这两个信号之间的主要区别在于,一个是关于文本光标位置在 QTextEdit 元素中移动,另一个是关于通过键盘、鼠标交互改变 selected 文本。此外,这两个信号都可能被触发,因为两个光标位置都可能发生变化,select编辑文本也是如此。
一个QTextCursor has two positions relevant to these signals, anchor and position.
selectionChanged
将在锚点或位置更改时发出 1.
cursorPositionChanged
位置改变时会发出。
QTextCursor
is modeled on the way a text cursor behaves in a text editor, providing a programmatic means of performing standard actions through the user interface. A document can be thought of as a single string of characters. The cursor's currentposition()
then is always either between two consecutive characters in the string, or else before the very first character or after the very last character in the string.A
QTextCursor
also has ananchor()
position. The text that is between theanchor()
and theposition()
is the selection. Ifanchor() == position()
there is no selection.
- 除非“无选择”改变位置,即改变前后相等
游标是一个迭代器:这是您可以 "point to" 文档中某些内容的唯一方式。因此,要创建一个选择,您需要两个光标位置 - anchor()
位置和当前 position()
。这就是游标同时具有它们的原因(它不是最干净的 API,但可行的选择 API 会复制很多游标的 API)。由于光标用于指向文档,因此选择取决于它们,并且您无法在没有光标的情况下定义选择。为清楚表明游标是构建选择的基础,您只能将选择创建为游标的 属性。
由于游标是迭代器,因此一个文档上可以有多个游标。 cursorPositionChanged()
仅在主光标上发出,代表可见的闪烁光标。因此,此信号仅在 QTextEdit
上可用,它维护自己的主游标。文档本身没有那个信号,因为没有一个光标是特殊的。因此你有 QTextDocument::cursorPositionChanged(const QTextCursor &)
信号。
同样,一个文档可以有多个选择 - 一个选择是一个 属性 游标,但只有主游标的选择可见,并且 selectionChanged()
仅在主游标时发出选择改变。可以存在其他选择,因为选择只是文档中的一个范围,使用不一定要可见的选择有很多原因。
您可以在没有 QTextEdit
的情况下以编程方式对文档进行所有编辑,因此 QTextDocument
甚至没有 selectionChanged
信号,因为没有单一的选择赞成。不过,当光标的选择发生变化时,它会发出光标变化信号。
要理解这些概念,仅查看 QTextDocument
并了解其 API 与 QTextEdit
的 API 之间的差异会有所帮助。