How to append text to QPlainTextEdit without adding newline, and keep scroll at the bottom?

I’ll just quote what I found here:

http://www.jcjc-dev.com/2013/03/qt-48-appending-text-to-qtextedit.html


We just need to move the cursor to the end of the contents in the QTextEdit and use insertPlainText. In my code, it looks like this:

myTextEdit->moveCursor (QTextCursor::End);
myTextEdit->insertPlainText (myString);
myTextEdit->moveCursor (QTextCursor::End);

As simple as that. If your application needs to keep the cursor where it was before appending the text, you can use the QTextCursor::position() and QTextCursor::setPosition() methods, or

just copying the cursor before modifying its position [QTextCursor QTextEdit::textCursor()] and then setting that as the cursor [void QTextEdit::setTextCursor(const QTextCursor & cursor)].

Here’s an example:

QTextCursor prev_cursor = myTextEdit->textCursor();
myTextEdit->moveCursor (QTextCursor::End);
myTextEdit->insertPlainText (myString);
myTextEdit->setTextCursor (&prev_cursor);

Leave a Comment