Search code examples
pythonqtpyqt5qtextbrowser

Python - pyqt5 - clear selections of QTextBrowser


I want to clear the selection in QTextBrowser (clear the highlighting).

self.textBrowser.textCursor().clearSelection()

Above code is not working for me.


Solution

  • Try this:

    cursor = self.textBrowser.textCursor()
    cursor.clearSelection()
    self.textBrowser.setTextCursor(cursor)
    

    The cursor that is returned by self.textBrowser.textCursor() is only a copy of the textCursor being used. To apply the changes to the text browser, you must operate on the copy and then set the text browsers textCursor to the modified version.