title: PyQt5 - Changing font color under cursor
body: <p>I'm trying to change the color (Red) of the font that will be highlighted. The problem I'm facing is, as soon as I highlight the first word, <strong><em>all the following text becomes Red.</em></strong></p>
  
  <pre><code>def cursorPosition(self):
      logging.debug("Cursor Positiong changed")
      self.cursor = self.ui.captureWindow.textCursor()
  
      if self.cursor.hasSelection():
          fmt = QTextCharFormat()
          fmt.setForeground(Qt.red)
  
          logging.debug(self.cursor.selectedText())
  
          # We insert the new text, which will override the selected text
          self.cursor.insertText(self.cursor.selectedText(), fmt)
          # txt = '&lt;p style="color:red;"&gt;' + self.cursor.selectedText() + '&lt;/p&gt; '
          # self.cursor.insertHtml(txt)
  
          # And set the new cursor
          self.ui.captureWindow.setTextCursor(self.cursor)
  </code></pre>
  
  <p>I've tried doing it with two methods, <code>insertText</code> and <code>insertHtml</code>.
  <code>insertHtml</code> <em>works if there is a space after the closing tag</em>. Remove that space, it behaves the same way as <code>insertText</code>, everything will be red after the first highlight.</p>
  
  <p><a href="https://i.stack.imgur.com/SCrpV.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SCrpV.gif" alt="enter image description here"></a></p>