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

Unexpected behavior if QTextEdit::keyPressEvent enclosed in cursor beginEditBlock/endEditBlock

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.14.2
    • GUI: Text handling
    • None
    • Linux/X11

    Description

      I created a subclass of QTextEdit, overrided keyPressEvent and enclosed everything inside beginEditBlock/endEditBlock cursor commands (I want to do some custom stuff, and I don't want this stuff to clutter undo history).

       

      class CustomTextEdit : public QTextEdit
      {
      public:
        using QTextEdit::QTextEdit;
        void keyPressEvent(QKeyEvent *e) override }
          textCursor().beginEditBlock();
          // Do some custom stuff
          QTextEdit::keyPressEvent(e);
          textCursor().endEditBlock();
        }
      };

      Then I add some large text so that it does not fit to the view and scrollbar appears. After that I move cursor to the very end of the document, and press Enter to create a new line - scroll immediately jumps to the very beginning of the document. When I start typing, the scroll returns to the cursor.

      The same happens if I try setting cursor right after I inserted a block (inside a beginEditBlock/endEditBlock; here "edit" is a QTextEdit):

      auto cursor = edit->textCursor();
      cursor.beginEditBlock();
      cursor.insertBlock();
      edit->setTextCursor(cursor);
      cursor.endEditBlock();

      I've attached minimal working example demonstrating this issue.

      There is also a workaround: I can call "ensureCursorVisible" after "textCursor().endEditBlock()" (in the first code snippet), and it works fine. For the second snippet, it's sufficient to swap two lines. 

      Attachments

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

        Activity

          People

            esabraha Eskil Abrahamsen Blomfeldt
            poboiko Igor Poboiko
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes