[PyKDE] QString to Python string conversion trouble

Phil Thompson phil at river-bank.demon.co.uk
Tue Oct 23 10:16:05 BST 2001


>===== Original Message From Andreas Gerstlauer <gerstl at ics.uci.edu> =====
>Hi!
>
>I am having problems with QString to Python string conversion
>ever since the latest update of PyQt (3.0pre4; together with
>Python 2.0 and QT 3.0.0). See the following trace:
>
>---- snipp ----
>
>% python
>Python 2.0 (#10, Feb 13 2001, 13:25:47)
>[GCC 2.95.2 19991024 (release)] on sunos5
>Type "copyright", "credits" or "license" for more information.
>>> import qt
>>> l = "Test\nTest"
>>> print l
>Test
>Test
>>> s = qt.QString(l)
>>> print str(s)
>Test\012Test
>>>
>
>---- snipp ----
>
>This is not the behavior we used to have before and it is not
>the behavior I would expect (back-and-forth conversion between
>QStrings and Python strings should be transparent). Is this
>new behavior intended? If so, how do I get the "real" string
>out of a QString object now (apparently _not_ the str() function
>any more as that gives me some sort of encoded string)?

Damn. The change was required to handle the case where the QString contained 
Unicode characters > 128 so that it wouldn't raise an exception. Note that the 
two strings are logically the same - one encodes newline as \n, the other as 
\012.

Strictly speaking the new behaviour is correct - but I don't want to break 
existing code.  It's wrong to say that QString/Python string conversion should 
be transparent - they hold different (but similar) types of data.

Is the change only cosmetic, or does it break things?

The only thing I could do is to check the QString and use the old conversion 
if all the characters are ASCII, and the new one if any character is 
non-ASCII.

Phil





More information about the PyQt mailing list