Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Como implementar "reemplazar todos" en un QPlainTextEdit

Así es co­mo se im­ple­men­ta 'reem­pla­zar to­do­s' en un QPlain­TextE­dit (o un QTextE­di­t, pro­ba­ble­men­te) usan­do Py­Qt (es si­mi­lar pa­ra 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 ma­ne­ras más fá­ci­le­s, pe­ro es­ta ha­ce que to­do apa­rez­ca en una so­la ope­ra­ción en la pi­la un­do­/­re­do y esas co­sas.

Andrés / 2010-02-15 23:42:

Typo en el título ;)

Roberto Alsina / 2010-02-15 23:49:

Corrigiendo...

JoeNotCharles / 2010-02-16 01:01:

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?)

Roberto Alsina / 2010-02-16 01:12:

No because it only does one pass from the cursor position forward.

Scott / 2010-02-16 02:17:

I'm curious to learn as to why you didn't using a QRegExp in place of the while loop?

Roberto Alsina / 2010-02-16 02:25:

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)

Thomas / 2010-02-16 07:23:

Curious; what (programming) language is this?

Roberto Alsina / 2010-02-16 09:41:

That is Python (http://www.python.org) using a library called PyQt

mat / 2011-02-23 13:07:

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));
}

phone number lookup / 2011-12-03 22:20:

this is really interesting viewpoint on the subject i might add

phone number lookup / 2011-12-03 22:30:

this is really interesting viewpoint on the subject i might add

employment background check / 2011-12-27 23:23:


Hi very nice article

employment background check / 2011-12-27 23:28:

Man ... Beautiful . Amazing ... I will bookmark your website and use the your RSS feed also

cell phone lookup / 2012-01-17 05:52:


Your blog has the same post as another author but i like your better

cell phone lookup / 2012-01-17 05:53:


Your blog has the same post as another author but i like your better


Contents © 2000-2024 Roberto Alsina