How to implement "replace all" in a QPlainTextEdit
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).
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.
Typo en el título ;)
Corrigiendo...
Doesn't this break if old is a substring of new? (ie. if you replace "abc" with "abc123", won't it repeatedly expand "abc123", "abc123123", "abc123123123", etc?)
No because it only does one pass from the cursor position forward.
I'm curious to learn as to why you didn't using a QRegExp in place of the while loop?
This way the code is trivial. With a regexp I need to escape the text I want to replace, and still need to loop since QTextDocument.find only finds the first time it appears (unless I am reading the docs wrong)
Curious; what (programming) language is this?
That is Python (http://www.python.org) using a library called PyQt
void Editor::SlotReplaceAll(QString findString, QRegExp findExpr, bool isExpr, QString replaceString, bool caseSensitively, bool wholeWords)
{
QTextDocument *doc = document();
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
cursor.movePosition(QTextCursor::Start);
QTextCursor newCursor = cursor;
quint64 count = 0;
QTextDocument::FindFlags options;
if (caseSensitively) options = options | QTextDocument::FindCaseSensitively;
if (wholeWords) options = options | QTextDocument::FindWholeWords;
if (!findString.isEmpty())
{
while (true)
{
if (isExpr)
newCursor = doc->find(findExpr, newCursor, options);
else
newCursor = doc->find(findString, newCursor, options);
if (!newCursor.isNull())
{
if (newCursor.hasSelection())
{
newCursor.insertText(replaceString);
count++;
}
}
else
{
break;
}
}
}
cursor.endEditBlock();
QMessageBox::information(this, tr("ReplaceAll"), tr("%1 occurrence(s) were replaced.").arg(count));
}
this is really interesting viewpoint on the subject i might add
this is really interesting viewpoint on the subject i might add
Hi very nice article
Man ... Beautiful . Amazing ... I will bookmark your website and use the your RSS feed also
Your blog has the same post as another author but i like your better
Your blog has the same post as another author but i like your better