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

Maziar Parsijani maziar.parsijani at gmail.com
Sat Sep 15 21:22:56 BST 2018

Hi Maurizio
I removed the editblocks but it changes about 1 second in speed.And about
the other method I have to say I don't know much about Qtextdocument would
you please explain it more.
I use to retrieve data from sqlite in a qtextdit like this:

for row in self.all_rows:
self.SearchResults.append('{0} )- {1} m ={3}, A={2}'.format(b, row[2],
row[1], row[0]))

The "self.searchresults" is qtextedit.Now I want know that you mean I do :
doc = QtGui.QTextDocument
doc.setDefaultStyleSheet(<span class="highlight">found</span>)
QTextEdit textEdit

I don't know much about Html and how to setDefaultStyleSheet or how to
highlight in this way.

On Sat, Sep 15, 2018 at 12:51 PM Maurizio Berti <maurizio.berti at gmail.com>

> 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/20180916/a8d0bf6e/attachment.html>

More information about the PyQt mailing list