[PyKDE] Python Unicode Objects and Qt Unicode strings

Boudewijn Rempt bsarempt at rempt.xs4all.nl
Sat Dec 16 22:23:05 GMT 2000


I'm playing with Qt Unicode strings and Python Unicode strings, and think
I've come across a problem. The conversion from QString to Python Unicode
takes as codec the one defined in site.py - and this is by default ASCII,
meaning that any character beyond the ASCII range gives an UnicodeError:

Traceback (most recent call last):
  File "uni.py", line 14, in ?
    print qsu
  File "/usr/local/lib/python2.0/site-packages/qt.py", line 948, in
__str__
    return str(self.sipThis)
UnicodeError: ASCII encoding error: ordinal not in range(128)

I can't find a way of telling the unicode() function to use another codec,
say utf-8...  This is my test script:

from qt import QString, QChar
import string

begin=string.atoi("0250",16)
end=string.atoi("02AF",16)

qsu=QString()
psu=u""
for wch in range(begin, end + 1):
  qsu.append(QString(QChar(wch)))
  psu=psu + unichr(wch)
  
print psu.encode("utf-8")
print unicode(qsu, "utf-8")
I'd really hate to have to tell users of Kura that they will have to
change the site.py file. Would it be a good idea to have QString
return UTF8 encoded text when converted with unicode()? Or perhaps,
but I don't know whether that is possible, make QString work with
the codec options of unicode().





More information about the PyQt mailing list