Search code examples
python-3.xpyqtpysideqtexteditdeselect

PyQt or PySide: QTextEdit deselect all


I'm using PySide(PyQt is fine as well) and I want to deselect everything inside a QTextEdit. Selecting everything is very easy and it's done by self.textedit.selectAll(), but I can't find a simple way to deselect everything. Is there a straightforward way to do it that I'm not aware of or is it more complicated than that?

Thanks.


Solution

  • You want to first get the QTextCursor for the QTextEdit

    my_text_cursor = my_text_edit.textCursor()
    

    Then clear the selection of the QTextCursor

    my_text_cursor.clearSelection()
    

    Then update the QLineEdit with the new QTextCursor (see documentation for QTextEdit.textCursor() which indicates updating the returned QTextCursor does not actually affect the QTextEdit unless you also call the following)

    my_text_edit.setTextCursor(my_text_cursor)