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, all the following text becomes Red.
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 = '<p style="color:red;">' + self.cursor.selectedText() + '</p> '
# self.cursor.insertHtml(txt)
# And set the new cursor
self.ui.captureWindow.setTextCursor(self.cursor)
I've tried doing it with two methods, insertText
and insertHtml
.
insertHtml
works if there is a space after the closing tag. Remove that space, it behaves the same way as insertText
, everything will be red after the first highlight.