1

In the doc for QTextEdit, I see two signals defined:

void cursorPositionChanged()
void selectionChanged()

The doc for the former says only "This signal is emitted whenever the position of the cursor changed." The doc for the latter says only "This signal is emitted whenever the selection changes." Elsewhere in the QTextEdit doc, it makes it sound like these two concepts are identical; for example, "Selection of text is handled by the QTextCursor class, which provides functionality for creating selections, retrieving the text contents or deleting selections." So what is the difference between these two signals??

2
  • 3
    I would expect the cursor to be the "edit position" in the text, i.e. where the user's text cursor is currently (if they typed, where would text be added). I would expect the selection to be whatever text is currently selected. See QTextCursor
    – Justin
    Jul 16, 2019 at 18:26
  • 1
    I seem to recall that the cursor is where any text that is typed would be inserted, while the selection is a range of text that you have selected. You always have a cursor, but you might not have anything selected. I think the two are related because you move the cursor to create the selection.
    – Ian4264
    Jul 16, 2019 at 18:27

3 Answers 3

3

A cursor is an iterator: it's the only way you can "point to" something in the document. So, to create a selection, you'll need two cursor positions - the anchor() position, and the current position(). That's why a cursor has both of them (it's not the cleanest API, but a viable selection API would replicate a lot of a cursor's API). Since cursors are used to point into the document, a selection depends on them, and you couldn't define the selection without cursors. To make that clear that a cursor is the basis upon which selections are built, you can only create selections as a property of a cursor.

Since a cursor is an iterator, there can be multiple cursors on a document. The cursorPositionChanged() is emitted only on the primary cursor, that represents the visible blinking cursor. Thus, this signal is only available on QTextEdit, which maintains its own primary cursor. The document itself doesn't have that signal, since no single cursor is special. Thus you have the QTextDocument::cursorPositionChanged(const QTextCursor &) signal.

Similarly, there can be multiple selections on a document - a selection is a property of a cursor, but only the primary cursor's selection is visible, and selectionChanged() is emitted only when that primary selection changes. Other selections can exist, because a selection is just a range in a document, and there are many reasons to use selections that don't have to be visible.

You can do all the editing on a document programmatically, without a QTextEdit, and thus QTextDocument doesn't even have a selectionChanged signal, because there's no single selection to favor. It'll emit a cursor change signal when the cursor's selection changes, though.

To understand these concepts, it helps to look at QTextDocument only, and see the differences between its API, and QTextEdit's API.

1

I think the best way to explain the difference is to show some minimal, but complete example. First of all, let us assume we have some textEdit, and connect it to the signals mentioned:

connect(ui->textEdit, &QTextEdit::selectionChanged, this, []
{
    qDebug() << "Selection Changed";
});

connect(ui->textEdit, &QTextEdit::cursorPositionChanged, this, []
{
    qDebug() << "Cursor Position Changed";
});

Now there can be the following use cases which will cause signals to get triggered, thus output will vary based on that:

  1. You can enter some text by keyboard without selecting it. In this case, only cursor position will change, thus &QTextEdit::cursorPositionChanged will get triggered which will result in "Cursor Position Changed" text getting printed.
  2. You can use Ctrl+A combination on Windows to select all text. In this case &QTextEdit::selectionChanged will get triggered, but &QTextEdit::cursorPositionChanged will not.
  3. On Windows, you can click left mouse button, and then move it around in such a way that text would not get actually selected. In this case, you would get &QTextEdit::selectionChanged triggered multiple times. Not sure why (maybe it is a Qt issue?)

In other words, I think you should just try this example, and see how different outputs are retrieved in order to see and feel the difference. And finally, the main difference between these two signals is that one is about text cursor position moving in the QTextEdit element, and another is about selected text by keyboard, by mouse interaction getting changed. Also, both of these signals might be triggered, since both cursor position might change, and selected text as well.

1
  • I have made this the accepted answer because the behavior seems to be sufficiently inconsistent and bizarre that "try it and see" is the only answer that seems to be correct. Unfortunate. I find that I have to listen to both signals to actually get useful information.
    – bhaller
    Dec 6, 2019 at 14:37
1

A QTextCursor has two positions relevant to these signals, anchor and position.

selectionChanged will be emitted when either anchor or position changes1.

cursorPositionChanged will be emitted when position changes.

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.

  1. Except when the "no selection" changes position, i.e. they are equal before and after the change.
1
  • 1
    I had accepted this answer, but unfortunately it does not appear to be quite correct. For example, if you have a zero-width selection (just an insertion point) and press left-arrow to move one position to the left, selectionChanged is not emitted (following footnote 1 of this answer), but if you click to move the insertion point to a new position, selectionChanged is emitted (contradicting footnote 1 of this answer). This behavior appears to be inconsistent, bizarre, and rather useless. I have concluded that I have to listen to both signals to get useful information.
    – bhaller
    Dec 6, 2019 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.