[PyQt] Problems with UTF-8 characters in UI / pyuic4

Giuseppe Corbelli giuseppe.corbelli at copanitalia.com
Fri Jan 21 12:26:49 GMT 2011


QT 4.6.3, PyQt 4.7.7, Windows XP, ActivePython 2.6.5

I have a form with a non-standard characted (the symbol for microliter). The
XML UI is something like:

<?xml version="1.0" encoding="UTF-8"?>
....
    <property name="text">
     <string>Ansa 1μl</string>
    </property>
....

The elementTree correctly parses the structure, however when I'm "compiling"
the UI with
uic.compileUi(source, pyfile, True)

I get an error:
Traceback (most recent call last):
  File
"/usr/local/lib/python2.6/dist-packages/doit-0.9.0-py2.6.egg/doit/action.py",
line 270, in execute
    returned_value = self.py_callable(*self.args,**kwargs)
  File "/home/lory/workspace/WaspCore/dodo.py", line 60, in forms_actions
    uic.compileUi(source, pyfile, True)
  File "/usr/lib/pymodules/python2.6/PyQt4/uic/__init__.py", line 128, in
compileUi
    winfo = compiler.UICompiler().compileUi(uifile, pyfile)
  File "/usr/lib/pymodules/python2.6/PyQt4/uic/Compiler/compiler.py", line 66,
in compileUi
    w = self.parse(input_stream)
  File "/usr/lib/pymodules/python2.6/PyQt4/uic/uiparser.py", line 842, in parse
    actor(elem)
  File "/usr/lib/pymodules/python2.6/PyQt4/uic/uiparser.py", line 689, in
createUserInterface
    self.traverseWidgetTree(elem)
  File "/usr/lib/pymodules/python2.6/PyQt4/uic/uiparser.py", line 667, in
traverseWidgetTree
    handler(self, child)
  File "/usr/lib/pymodules/python2.6/PyQt4/uic/uiparser.py", line 163, in
createWidget
    self.stack.push(self.setupObject(widgetClass(elem), parent, elem))
  File "/usr/lib/pymodules/python2.6/PyQt4/uic/uiparser.py", line 136, in
setupObject
    self.wprops.setProperties(obj, branch)
  File "/usr/lib/pymodules/python2.6/PyQt4/uic/properties.py", line 363, in
setProperties
    prop_value = self.convert(prop, widget)
  File "/usr/lib/pymodules/python2.6/PyQt4/uic/properties.py", line 328, in
convert
    return func(prop[0], **args)
  File "/usr/lib/pymodules/python2.6/PyQt4/uic/properties.py", line 102, in
_string
    return self._cstring(prop)
  File "/usr/lib/pymodules/python2.6/PyQt4/uic/properties.py", line 113, in
_cstring
    return str(prop.text)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u03bc' in position
2: ordinal not in range(128)

u"\u03bc" is the unicode for the symbol I was talking above (microliter)

I've found that in pyuic.uic.properties.py I have
def _cstring(self, prop):
        return str(prop.text)

It can be changed to
def _cstring(self, prop):
	if isinstance(prop.text, unicode):
            return prop.text.encode("UTF-8")
        else:
            return str(prop.text)

And this solves my issue ONLY IF the text is marked as "Translatable". If the
"Translatable" is OFF the prop.text is not unicode and so the string contains
some random shit depending on locale and whatever. This will need some more
work...
-- 
            Giuseppe Corbelli
WASP Software Engineer, Copan Italia S.p.A
Phone: +390303666104  Fax: +390302659932
E-mail: giuseppe.corbelli at copanitalia.com


More information about the PyQt mailing list