[PyQt] including Unicode in QListWidget

Knacktus knacktus at googlemail.com
Sun Jun 17 17:42:54 BST 2012

Am 17.06.2012 18:29, schrieb David Beck:
> I am trying to build a GUI for navigating through a large XML database on a Mac running OS 10.7, Python 3.3, PyQt 4. I want to get a list of the text in all of the nodes called<Orth>  and put them into a QListWidget called "hLexNav". To do this, I wrote the following bit of code (this isn't the whole thing, just the parts that are supposed to add items to the listbox):
> import sys
> from PyQt4 import QtCore, QtGui
> from xml.dom import minidom
> import xml.etree.ElementTree as etree
> from fieldbookGui import Ui_Fieldbook
> import images
> import btnCmds
> class MyForm(QtGui.QMainWindow):
>    def __init__(self, parent=None):
>      QtGui.QWidget.__init__(self, parent)
>      self.ui = Ui_Fieldbook()
>      self.ui.setupUi(self)
>      xmltree = etree.parse('BabyDb.xml')
>      root = xmltree.getroot()
>      for child in root:
>        self.ui.hLexNav.addItem(child.findtext('Orth'))
> The first 25 items that are returned by child.findtext('Orth') are:
> ['a:', 'a:cháj', 'a:chulá:', "a:hé:xtu'", 'a:ho:tán', 'a:kús', "a:li:ma'htín", 'a:li:stá:n', 'a:má', "a:ma'ha:'pi'tzí'n", 'a:mixtzayán', 'a:nanú:', 'a:tú:n', 'a:tzá:', "a:tzemá'j", 'a:xí:lh', 'a:xtúm', 'a:xú:x', "a:'hála'", "a:'j", "a:'jmá", "a:'jnanú:", "a:'jtzá:", "a:'jtzananú:", "a:'kní:"]
> In the QListWidget created by this code, I see only items corresponding to those elements that do not contain accented vowels (here, those that don't contain "á", "ú", etc.); items that correpsond to strings with accented vowels are left empty. Further experimentation with addItem( ), addItems(), and insertItem( ) show that any string that contains an non-ASCII character results in an empty Item being inserted into the QListWidget.
> Any ideas about what is going on would be appreciated.

Are you 100 % sure that unicode is handled properly while reading the 
xml? I never had problems with unicode and PyQt but I strictly using 
unicode strings only in my apps.

This for example works for me (Python 2.7):

# -*- coding: utf-8 -*-

if __name__ == "__main__":

     import sys
     from PyQt4.QtGui import *
     app = QApplication(sys.argv)
     list_widget = QListWidget()
     list_widget.addItem(u"ÄÜ^°  là lÁ")

> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

More information about the PyQt mailing list