--- author: '' category: '' date: 2010/02/15 18:47 description: '' link: '' priority: '' slug: BB870 tags: programming, pyqt, python, qt title: How to implement "replace all" in a QPlainTextEdit type: text updated: 2010/02/15 18:47 url_type: '' --- This is not interesting for almost noone, but since my google-fu didn't let me find it and it was a bit of a pain to do: This is how you implement 'replace all' in a QPlainTextEdit (or a QTextEdit, for that matter) using PyQt (similar for C++ of course). .. code-block:: python def doReplaceAll(self): # Replace all occurences without interaction # Here I am just getting the replacement data # from my UI so it will be different for you old=self.searchReplaceWidget.ui.text.text() new=self.searchReplaceWidget.ui.replaceWith.text() # Beginning of undo block cursor=self.editor.textCursor() cursor.beginEditBlock() # Use flags for case match flags=QtGui.QTextDocument.FindFlags() if self.searchReplaceWidget.ui.matchCase.isChecked(): flags=flags|QtGui.QTextDocument.FindCaseSensitively # Replace all we can while True: # self.editor is the QPlainTextEdit r=self.editor.find(old,flags) if r: qc=self.editor.textCursor() if qc.hasSelection(): qc.insertText(new) else: break # Mark end of undo block cursor.endEditBlock() There are other, easier ways to do it, but this one makes it all appear as a single operation in the undo stack and all that.