Como implementar "reemplazar todos" en un QPlainTextEdit
Así es como se implementa 'reemplazar todos' en un QPlainTextEdit (o un QTextEdit, probablemente) usando PyQt (es similar para C++).
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()
Hay otras maneras más fáciles, pero esta hace que todo aparezca en una sola operación en la pila undo/redo y esas cosas.
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