[PyQt] General questions on parsing large QStrings

Phil Thompson phil at riverbankcomputing.com
Mon May 13 08:33:24 BST 2013


On Sun, 12 May 2013 22:12:00 -0700, David Cortesi <davecortesi at gmail.com>
wrote:
> Thanks to you and Mathias for the prompt replies.
> 
>  >     docstring = unicode( myEditor.toPlainText() )
>> In PyQt5 toPlainText() will return a str object for Python3 and a
unicode
>> object for Python2.
>>
> 
> And, as Mathias says, it is a copy?...
> That is not IMO a good design choice. At least if toPlainText
> returns a const QString reference, one can then use r/o QString
> methods like count(), contains(), indexOf etc, without penalty.
> Also one could provide it to a QRegExp, e.g.:
> 
>     j = qre.indexIn( myEditor.toPlainText() )
>     while j >= 0 :
>         # ...do something with qre.cap(0)...
>         j = qre.indexIn( myEditor.toPlainText() , j )
> 
> This is more or less the patther of a syntax highlighter
> as well as other possible applications -- and it
> would be a catastrophe with a large doc when every
> toPlainText call does a new memcopy.

Which is why you wouldn't do it, even in C++. You would use QTextDocument,
QSyntaxHighlighter etc.

> In short, I urge you to leave the conversion to Python str
> in the programmer's hands, e.g. via unicode() or str().
> 
> Or, find a way to support those "STL-style iterators"
> that are currently omitted...?

The behaviour you are anticipating (but not demonstrated) is a problem has
been the standard behaviour of Python3 and PyQt4 for 4 years without
anybody complaining.

If it proves that it is a real problem then I will re-introduce the
QString v1 API. As it won't be the default for any Python version, that can
be done without affect compatibility.

Phil


More information about the PyQt mailing list