Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

PyQt4 4.4.4 : a bug with highlightBlock ?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


alexis.flesch at free

Nov 18, 2009, 2:47 AM

Post #1 of 3 (255 views)
Permalink
PyQt4 4.4.4 : a bug with highlightBlock ?

Hello everybody,

I've been trying to do some syntax highlighting using PyQt4. I ported
the example given in the documentation of Qt4 to Python. It works fine
on my computer at work (which has PyQt4 version 4.3.3) but doesn't on
my home computer (which has version 4.4.4) : it gets stuck in an
infinite loop.
Here is the code :

class MyHighlighter(QtGui.QSyntaxHighlighter):
def __init__(self, edit):
QtGui.QSyntaxHighlighter.__init__(self,edit)

def highlightBlock(self, text):
myClassFormat = QtGui.QTextCharFormat()
myClassFormat.setFontWeight(QtGui.QFont.Bold)
myClassFormat.setForeground(QtCore.Qt.darkMagenta)
pattern = "\\b[A-Z_]+\\b"

expression = QtCore.QRegExp(pattern)
index = text.indexOf(expression);
while (index >= 0):
length = expression.matchedLength()
self.setFormat(index, length, myClassFormat)
index = text.indexOf(expression, index + length)


What am I missing ? Is this a known bug of version 4.4.4 ?

Thank you,

Alexis.
--
http://mail.python.org/mailman/listinfo/python-list


david at boddie

Nov 19, 2009, 3:55 PM

Post #2 of 3 (239 views)
Permalink
Re: PyQt4 4.4.4 : a bug with highlightBlock ? [In reply to]

On Wednesday 18 November 2009 11:47, Snouffy wrote:

> I've been trying to do some syntax highlighting using PyQt4. I ported
> the example given in the documentation of Qt4 to Python. It works fine
> on my computer at work (which has PyQt4 version 4.3.3) but doesn't on
> my home computer (which has version 4.4.4) : it gets stuck in an
> infinite loop.

This is a known issue. There are examples distributed with PyQt that should
have been updated to use a slightly different approach.

> Here is the code :
>
> class MyHighlighter(QtGui.QSyntaxHighlighter):
> def __init__(self, edit):
> QtGui.QSyntaxHighlighter.__init__(self,edit)
>
> def highlightBlock(self, text):
> myClassFormat = QtGui.QTextCharFormat()
> myClassFormat.setFontWeight(QtGui.QFont.Bold)
> myClassFormat.setForeground(QtCore.Qt.darkMagenta)
> pattern = "\\b[A-Z_]+\\b"
>
> expression = QtCore.QRegExp(pattern)
> index = text.indexOf(expression);
> while (index >= 0):
> length = expression.matchedLength()
> self.setFormat(index, length, myClassFormat)
> index = text.indexOf(expression, index + length)

You need to change the indexOf() calls to indexIn() calls on the QRegExp
object:

index = expression.indexIn(text, index + length)

> What am I missing ? Is this a known bug of version 4.4.4 ?

I think there was a behavioural change at some point that affected regular
expression searching in QStrings.

David
--
http://mail.python.org/mailman/listinfo/python-list


alexis.flesch at gmail

Nov 20, 2009, 9:58 AM

Post #3 of 3 (241 views)
Permalink
Re: PyQt4 4.4.4 : a bug with highlightBlock ? [In reply to]

> You need to change the indexOf() calls to indexIn() calls on the QRegExp
> object:
>
>   index = expression.indexIn(text, index + length)

Thank you so much ! After looking a bit more I found out that when
using indexOf the command :
length = expression.matchedLength()
was the one causing a problem. Thus I also had to change the line :
index = text.indexOf(expression)
to :
index = expression.indexIn(text)

Now it works like a charm ! I'll test it at work where we have PyQt
4.3.3.

Thanks again !
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.