Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Modifying one QTextCursor affects all other QTextCursors for that document
Qt 6.11 is out! See what's new in the release blog

Modifying one QTextCursor affects all other QTextCursors for that document

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Drew W
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Drew W
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sirspot
        wrote on last edited by
        #3

        This problem still exists in Qt 6.9

        SGaistS 1 Reply Last reply
        0
        • S sirspot

          This problem still exists in Qt 6.9

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @sirspot Hi and welcome to devnet,

          Please provide a complete minimal compilable example that shows this behavior.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved