[PyQt] [PYQT] Increse the regex in QtCore.QRegExp

Maurizio Berti maurizio.berti at gmail.com
Sat Sep 15 09:20:29 BST 2018


I've used QTextDocuments just a little bit in the past, so I'm a bit rusty
on this.
First question is, why are you using editblocks? As far as I can tell,
editblocks are used for "edit" operations, meaning that each edit block is
a possible undo/redo operation. In my experience, this consumes a lot of
process, as each time you do that a new QUndoCommand will be pushed in the
QUndoStack. I had a similar problem for a program of mine which had a
function that randomized about 300 parameters: what I didn't realize at the
time was that, since each parameter change would push a QUndoCommand, it
froze the program for 5-10 seconds. So, I think it's possible that your
issue could be related to this.
I assume that you wouldn't need that, since you just want to highlight the
search results, so maybe you should just use QTextCursor.insertBlock
instead.

Also, have you tried to think about another approach? I don't know if the
text you show has to be editable or not, but if it doesn't, you could think
about using QTextDocument.setHtml and then use a stylesheet.
It could be something simple as:

self.myTextDocument.setDefaultStyleSheet('''
    .highlight {
        color: red;
    }'''

Let's say you the search key is "found", each instance of it will have to
be substituted with "<span class="highlight">found</span>". After that,
simply call setHtml with the contents, and that would be enough.

Maurizio


2018-09-15 9:31 GMT+02:00 Maziar Parsijani <maziar.parsijani at gmail.com>:

> Hi,
> I use this code in Qtextedit to find "sth" but the problem is when I have
> more data it is lack on speed.
> """
>
> cursor = self.SearchResults.textCursor()
> format = QtGui.QTextCharFormat()
> format.setBackground(QtGui.QBrush(QtGui.QColor("red")))
> pp = self.m
> pattern1 = r'\b(?:{})\b'.format(pp)
> regex = QtCore.QRegExp(pattern1)
> pos = 0
> index = regex.indexIn(self.SearchResults.toPlainText(), pos)
> tedad = 0
> while (index != -1):
>     cursor.beginEditBlock()
>     cursor.setPosition(index)
>     cursor.movePosition(QtGui.QTextCursor.NextWord, QtGui.QTextCursor.KeepAnchor, 1)
>     cursor.endEditBlock()
>     cursor.mergeCharFormat(format)
>     pos = index + regex.matchedLength()
>     index = regex.indexIn(self.SearchResults.toPlainText(), pos)
> """
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>



-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180915/e5f059d3/attachment.html>


More information about the PyQt mailing list