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

后端 未结 4 1289
时光取名叫无心
时光取名叫无心 2020-11-29 05:15

I need to append text to QPlainTextEdit without adding a newline to the text, but both methods appendPlainText() and appendHtml() adds

4条回答
  •  温柔的废话
    2020-11-29 05:47

    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);
    

提交回复
热议问题