PyQt5 - изменение цвета шрифта под курсором - PullRequest
0 голосов
/ 01 марта 2019

Я пытаюсь изменить цвет (красный) шрифта, который будет выделен.Проблема, с которой я сталкиваюсь, заключается в том, что, как только я выделю первое слово, весь следующий текст станет красным.

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)

Я попытался сделатьэто двумя способами, insertText и insertHtml.insertHtml работает, если после закрывающего тега есть пробел .Уберите это место, оно ведет себя так же, как insertText, все будет красным после первого выделения.

enter image description here

...