[PyQt] QsciLexerCustom class behaving differently in PyQt5?
Baz Walter
bazwal at ftml.net
Sun Nov 2 05:53:27 GMT 2014
On 02/11/14 03:52, Dominique Cyprès wrote:
> Hi,
>
> I'm having trouble understanding the differences in behavior between
> implementations of the QsciLexerCustom class in PyQt5 and PyQt4.
>
> I've been trying to work with a subclass of this, and was basing my work
> on some example code posted to this list on 12 July 2009 by Baz Walter,
> which I've copied to a pastebin:
> http://pastebin.com/eqRpBx0H
>
> After some trouble adapting this code for my PyQt5 project, I decided to
> simply make a few minimal adjustments to the code to make it run without
> errors in a Python 3 / PyQt5 environment:
> http://pastebin.com/pgS28wZ8
>
> Running the latter code results in a lexer that does not apply color to
> any of the sample source text at all.
>
> So you can see what I mean, here's a screenshot of the example code
> running on Python 2.7.6 with PyQt4:
> https://www.dropbox.com/s/zqcbsb9o3o8uky9/pyqt4-custom-lexer.png
>
> And here's a screenshot of the reworked example code running on Python
> 3.4.0 with PyQt5:
> https://www.dropbox.com/s/8elynzf3ms4bwg1/pyqt5-custom-lexer.png
>
> Is there some documentation that explains why this code behaves
> differently in PyQt5?
No need for documentation: you broke my code :)
Firstly, there's no unicode function in python3:
source = ''
if end > editor.length():
end = editor.length()
source = unicode(editor.text()).encode('utf-8')[start:end]
which should give you a strong hint that the above code never enters the
if block, and so the source will always be empty.
Try this, instead:
source = b''
if end > editor.length():
end = editor.length()
if end > start:
source = editor.text().encode('utf-8')[start:end]
And note that the source must be *bytes*, so you will also have to fix a
couple of other literals in this section:
if line.startswith(b'#'):
state = self.Comment
else:
# the following will style lines like "x = 0"
pos = line.find(b'=')
That should probably be enough to get the code working, but I can't test
it myself because I haven't got access to a PyQt5/QScintilla setup at
the moment.
--
Regards
Baz Walter
More information about the PyQt
mailing list