Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:39:13+00:00 2026-06-14T21:39:13+00:00

I need to append text to QPlainTextEdit without adding a newline to the text,

  • 0

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

I can do that manually with QTextCursor:

QTextCursor text_cursor = QTextCursor(my_plain_text_edit->document());
text_cursor.movePosition(QTextCursor::End);

text_cursor.insertText("string to append. ");

That works, but I also need to keep scroll at bottom if it was at bottom before append.

I tried to copy logic from Qt’s sources, but I stuck on it, because there actually QPlainTextEditPrivate class is used, and I can’t find the way to do the same without it: say, I don’t see method verticalOffset() in QPlainTextEdit.

Actually, these sources contain many weird (at the first look, at least) things, and I have no idea how to implement this.

Here’s the source code of append(): http://code.qt.io/cgit/qt/qt.git/tree/src/gui/widgets/qplaintextedit.cpp#n2763

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:39:14+00:00Added an answer on June 14, 2026 at 9:39 pm

    Ok, I’m not sure if my solution is actually “nice”, but it seems to work for me: I just made new class QPlainTextEdit_My inherited from QPlainTextEdit, and added new methods appendPlainTextNoNL(), appendHtmlNoNL(), insertNL().

    Please NOTE: read comments about params check_nl and check_br carefully, this is important! I spent several hours to figure out why is my widget so slow when I append text without new paragraphs.

    /******************************************************************************************
     * INCLUDED FILES
     *****************************************************************************************/
    
    #include "qplaintextedit_my.h"
    #include <QScrollBar>
    #include <QTextCursor>
    #include <QStringList>
    #include <QRegExp>
    
    
    /******************************************************************************************
     * CONSTRUCTOR, DESTRUCTOR
     *****************************************************************************************/
    
    QPlainTextEdit_My::QPlainTextEdit_My(QWidget *parent) :
       QPlainTextEdit(parent)
    {
    
    }
    
    QPlainTextEdit_My::QPlainTextEdit_My(const QString &text, QWidget *parent) :
       QPlainTextEdit(text, parent)
    {
    
    }        
    
    /******************************************************************************************
     * METHODS
     *****************************************************************************************/
    
    /* private      */
    
    /* protected    */
    
    /* public       */
    
    /**
     * append html without adding new line (new paragraph)
     *
     * @param html       html text to append
     * @param check_nl   if true, then text will be splitted by \n char,
     *                   and each substring will be added as separate QTextBlock.
     *                   NOTE: this important: if you set this to false,
     *                   then you should append new blocks manually (say, by calling appendNL() )
     *                   because one huge block will significantly slow down your widget.
     */
    void QPlainTextEdit_My::appendPlainTextNoNL(const QString &text, bool check_nl)
    {
       QScrollBar *p_scroll_bar = this->verticalScrollBar();
       bool bool_at_bottom = (p_scroll_bar->value() == p_scroll_bar->maximum());
    
       if (!check_nl){
          QTextCursor text_cursor = QTextCursor(this->document());
          text_cursor.movePosition(QTextCursor::End);
          text_cursor.insertText(text);
       } else {
          QTextCursor text_cursor = QTextCursor(this->document());
          text_cursor.beginEditBlock();
    
          text_cursor.movePosition(QTextCursor::End);
    
          QStringList string_list = text.split('\n');
    
          for (int i = 0; i < string_list.size(); i++){
             text_cursor.insertText(string_list.at(i));
             if ((i + 1) < string_list.size()){
                text_cursor.insertBlock();
             }
          }
    
    
          text_cursor.endEditBlock();
       }
    
       if (bool_at_bottom){
          p_scroll_bar->setValue(p_scroll_bar->maximum());
       }
    }
    
    /**
     * append html without adding new line (new paragraph)
     *
     * @param html       html text to append
     * @param check_br   if true, then text will be splitted by "<br>" tag,
     *                   and each substring will be added as separate QTextBlock.
     *                   NOTE: this important: if you set this to false,
     *                   then you should append new blocks manually (say, by calling appendNL() )
     *                   because one huge block will significantly slow down your widget.
     */
    void QPlainTextEdit_My::appendHtmlNoNL(const QString &html, bool check_br)
    {
       QScrollBar *p_scroll_bar = this->verticalScrollBar();
       bool bool_at_bottom = (p_scroll_bar->value() == p_scroll_bar->maximum());
    
       if (!check_br){
          QTextCursor text_cursor = QTextCursor(this->document());
          text_cursor.movePosition(QTextCursor::End);
          text_cursor.insertHtml(html);
       } else {
    
          QTextCursor text_cursor = QTextCursor(this->document());
          text_cursor.beginEditBlock();
    
          text_cursor.movePosition(QTextCursor::End);
    
          QStringList string_list = html.split(QRegExp("\\<br\\s*\\/?\\>", Qt::CaseInsensitive));
    
          for (int i = 0; i < string_list.size(); i++){
             text_cursor.insertHtml( string_list.at(i) );
             if ((i + 1) < string_list.size()){
                text_cursor.insertBlock();
             }
          }
    
          text_cursor.endEditBlock();
       }
    
       if (bool_at_bottom){
          p_scroll_bar->setValue(p_scroll_bar->maximum());
       }
    }
    
    /**
     * Just insert new QTextBlock to the text.
     * (in fact, adds new paragraph)
     */
    void QPlainTextEdit_My::insertNL()
    {
       QScrollBar *p_scroll_bar = this->verticalScrollBar();
       bool bool_at_bottom = (p_scroll_bar->value() == p_scroll_bar->maximum());
    
       QTextCursor text_cursor = QTextCursor(this->document());
       text_cursor.movePosition(QTextCursor::End);
       text_cursor.insertBlock();
    
       if (bool_at_bottom){
          p_scroll_bar->setValue(p_scroll_bar->maximum());
       }
    }
    

    I’m confused because in original code there are much more complicated calculations of atBottom:

    const bool atBottom =  q->isVisible()
                           && (control->blockBoundingRect(document->lastBlock()).bottom() - verticalOffset()
                               <= viewport->rect().bottom());
    

    and needScroll:

    if (atBottom) {
        const bool needScroll =  !centerOnScroll
                                 || control->blockBoundingRect(document->lastBlock()).bottom() - verticalOffset()
                                 > viewport->rect().bottom();
        if (needScroll)
            vbar->setValue(vbar->maximum());
    }
    

    But my easy solution seems to work too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to append text to RichTextBox, and need to perform it without making
I need append - to the present text box value without replacing present text.
I need to append text repeatedly to an existing file in Java. How do
I need to append some text to an input field...
HTML is <a>ref</a> I need to get <a>ref</a>text How can i do this? $('a').append('text')
I need to programatically append to a multi-page TIFF or PDF a new image.
I need to edit file , the main issue is to append text between
Good day. I found the example below, I need to append some more text
I need to append text to win32 edit control i have working function to
I need to append a text at the end of each line in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.