Change already appended text of document in Textbrowser
-
Hey everyone, I'm working with opencv object detection in pyqt5. Everytime something gets detected some text get's appended to a textlist which is displayed in a pyqt5 textbrowser.
I want to hightlight only the last generated text and want to change the color of the before generated text.
Has anyone an idea how to achieve that. How to change already appended text?
Thanks in advance -
Hey everyone, I'm working with opencv object detection in pyqt5. Everytime something gets detected some text get's appended to a textlist which is displayed in a pyqt5 textbrowser.
I want to hightlight only the last generated text and want to change the color of the before generated text.
Has anyone an idea how to achieve that. How to change already appended text?
Thanks in advance@levoxtrip Let's assume that what you indicate has been implemented so that the last row is highlighted, so if a new row is added then the previous row that is highlighted should not be highlighted but the new row, am I correct?
If so then you can use the following demo:
from PyQt5.QtCore import QDateTime, QTimer from PyQt5.QtGui import QColor, QTextCursor, QTextFormat from PyQt5.QtWidgets import QApplication, QTextBrowser class TextBrowser(QTextBrowser): def __init__(self, parent=None): super().__init__(parent) self.textChanged.connect(self.handle_text_changed) def handle_text_changed(self): previous_cursor = self.textCursor() self.moveCursor(QTextCursor.End) extraSelections = [] selection = QTextBrowser.ExtraSelection() lineColor = QColor("salmon") selection.format.setBackground(lineColor) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() extraSelections.append(selection) self.setExtraSelections(extraSelections) self.setTextCursor(previous_cursor) if __name__ == "__main__": import sys app = QApplication(sys.argv) w = TextBrowser() w.resize(640, 480) w.show() def handle_timeout(): w.append(QDateTime.currentDateTime().toString()) timer = QTimer(interval=1000, timeout=handle_timeout) timer.start() sys.exit(app.exec_())