Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-122969

QTextCursor::movePosition(NextCharacter, MoveAnchor) does not move cursor if a text is selected

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.5.2
    • GUI: Text handling
    • None
    • All

    Description

      QTextCursor::movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor) does not move cursor position IF a text is selected. See the code snippet below. In the first cursor move, it keeps anchor and selects first three letters and places cursor after the third letter, i.e. position == 3. The next movePosition() call should move the cursor to the next letter, i.e. it is expected to have position == 4. But in fact the cursor remains after the third letter (position == 3) and the only effect is that the operation only cleared the previous selection. This is very unexpected behavior given what is written in the documentation.

      #include <QApplication>
      #include <QDebug>
      #include <QTextEdit>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QTextEdit w;
          w.setPlainText("abcdef");
          QTextCursor tc = w.textCursor();
          tc.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 3);
          qInfo() << tc.position() << tc.selectedText(); // prints: 3 "abc"
          tc.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor);
          qInfo() << tc.position() << tc.selectedText(); // the cursor was not moved! prints: 3 ""
          w.setTextCursor(tc);
          w.show();
          return a.exec();
      }
      

      I understand there is a certain logic in what is happening. It seems that NextCharacter behaves the same as pressing right arrow key on keyboard. Because if there is a selection, then pressing the right key will clear the selection but not move the cursor. However documentation does not mention this and it gives the impression, that the the cursor should be moved regardless of the previous selection.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            esabraha Eskil Abrahamsen Blomfeldt
            vladimir.kraus Vladimir Kraus
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes