2

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.

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
b-ak
  • 141
  • 5
  • You want that if on a certain word the cursor is highlighted that word and if the cursor moves that word should no longer have the highlight. I am right? – eyllanesc Mar 01 '19 at 07:17
  • Yes. I highlight a word, its color should change for that word only. It can remain in the same color. But that color shouldn't continue like shown in the gif. – b-ak Mar 01 '19 at 07:25
  • Should the word that should be highlighted be the one under the cursor or the one that is pressed? – eyllanesc Mar 01 '19 at 07:33
  • I'm double clicking to highlight the word. So by definition, it will be the one that is pressed. Have I got that right? – b-ak Mar 01 '19 at 07:48

0 Answers0