title: QPlainTextEdit with multiple colours on a line
body: <h1>tl;dr:</h1>
  <p><code>QPlainTextEdit::appendPlainText(QString)</code> appends a newline to my text widget.  <code>QPlainTextEdit::insertPlainText(QString)</code>  doesn't seem affected by <code>setCurrentCharFormat()</code>.</p>
  <p>Is there a way to append text while listening to the current <code>QTextCharFormat</code>  without inserting a newline</p>
  <h1>Details</h1>
  <p>I have a &quot;terminal&quot; style widget which gets text from stdout from a child process and displays it in a <code>QPlainTextEdit</code>.</p>
  <p>Before I had colour content, I could simply do this:</p>
  <pre><code>void ProcessWidget::appendText(QString text)
  {
      m_textedit-&gt;appendPlainText(text);
  }
  </code></pre>
  <p>Colours appear in the text using the escape character <code>'\033'</code> followed by a colour.  I can detect the colour and set the pallette appropriately:</p>
  <pre><code>void ProcessWidget::appendText(QString text)
  {
      Qt::GlobalColor colour = GetColour(text);
  
      QTextCharFormat tf = m_textedit-&gt;currentCharFormat();
      tf.setForeground(QBrush(colour));
      m_textedit-&gt;setCurrentCharFormat(tf);
  
      m_textedit-&gt;appendPlainText(text);
  }
  </code></pre>
  <p>This works if there is only one colour per line, but if my colour changes half-way through each line, then I need to be a bit crazier:</p>
  <pre><code>std::map&lt;QString, Qt::GlobalColor&gt;   m_colours;
  QPlainTextEdit*                      m_textedit;
  ...
  
  void ProcessWidget::AppendText(QString text)
  {
      while(true)
      {
          int iColour = text.indexOf('\033');
  
          if (iColour == -1)
              break;
  
          QString pretext = text.mid(0, iColour);
  
          if (!pretext.isEmpty())
          {
              m_textedit-&gt;appendPlainText(pretext);
          }
  
          text.remove(0, iColour);
  
          for (auto pair : m_colours)
          {
              if ( text.startsWith(pair.first) )
              {
                  QTextCharFormat tf = m_textedit-&gt;currentCharFormat();
                  tf.setForeground(QBrush(pair.second));
                  m_textedit-&gt;setCurrentCharFormat(tf);
                  text.remove(0, pair.first.size());
                  break;
              }
          }
      }
  
      if (!text.isEmpty())
      {
          m_textedit-&gt;appendPlainText(text);
      }
  }
  </code></pre>
  <p>However, because I use <code>appendPlainText()</code>, each new colour found gives me a new line.</p>
  <p>I've tried replacing <code>appendPlainText()</code> with:</p>
  <pre><code>m_textedit-&gt;moveCursor (QTextCursor::End);
  m_textedit-&gt;insertPlainText(text);
  m_textedit-&gt;moveCursor (QTextCursor::End);
  </code></pre>
  <p>then adding <code>'\n'</code> at the end.  But in that case, I don't get any colours anymore.   I've also tried <code>appendHtml()</code> but that doesn't seem to make a difference.</p>