Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Modifying one QTextCursor affects all other QTextCursors for that document



  • The QTextCursor documentation implies that each cursor is independent, but when I set the position of one cursor it moves all cursors for the document.

    // cursors is of type QList<QTextCursor>
    // startBlock/endBlock are of type QTextBlock
    // The following code runs in a loop
    ...
    QTextCursor cursor(m_plainTextEdit->document());
    cursor.setPosition(startBlock.position(), QTextCursor::MoveAnchor);
    cursor.setPosition(endBlock.position() + endBlock.length() - 1, QTextCursor::KeepAnchor);
    ā€‹
    qDebug() << "Created cursor at " << cursor.position() << " with first cursor ending at " << (cursors.isEmpty() ? -1 : cursors.front().position());
    cursors.append(cursor);
    ...
    

    Output:
    1568832163.97167,2694,6 QT Debug: Created cursor at 93 with first cursor ending at -1
    1568832163.97266,2694,6 QT Debug: Created cursor at 130 with first cursor ending at 130
    1568832163.97564,2694,6 QT Debug: Created cursor at 953 with first cursor ending at 953
    1568832163.97663,2694,6 QT Debug: Created cursor at 1479 with first cursor ending at 1479
    1568832163.97961,2694,6 QT Debug: Created cursor at 2464 with first cursor ending at 2464

    I would expect every log message except the first to say the first cursor's position is 93, but it's whatever I moved the new cursor's position to this iteration. Am I doing something wrong, or is this intended behavior?



  • It looks like the cause is appending text in each iteration. The last cursor is at the end of the file, so Qt extends its selection as more text is added.


Log in to reply