[PyQt] including Unicode in QListWidget

David Beck dbeck at ualberta.ca
Sun Jun 17 21:55:57 BST 2012

> Message: 2
> Date: Sun, 17 Jun 2012 18:42:54 +0200
> From: Knacktus <knacktus at googlemail.com>
> To: pyqt at riverbankcomputing.com
> Subject: Re: [PyQt] including Unicode in QListWidget
> Message-ID: <4FDE090E.1070905 at googlemail.com>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 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?")
>     list_widget.show()
>     app.exec_()

Yes, it seems to be independent of the XML. For instance, I get the same thing when I run the little app below (the GUI is generated by pyuic4): 

import sys
from PyQt4 import QtCore, QtGui

    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_UTFWidget(object):
    def setupUi(self, UTFWidget):
        UTFWidget.resize(400, 300)
        self.centralWidget = QtGui.QWidget(UTFWidget)
        self.listWidget = QtGui.QListWidget(self.centralWidget)
        self.listWidget.setGeometry(QtCore.QRect(17, 9, 362, 241))
        self.menuBar = QtGui.QMenuBar(UTFWidget)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 22))
        self.menuUTF_test = QtGui.QMenu(self.menuBar)
        self.mainToolBar = QtGui.QToolBar(UTFWidget)
        UTFWidget.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtGui.QStatusBar(UTFWidget)


    def retranslateUi(self, UTFWidget):
        UTFWidget.setWindowTitle(QtGui.QApplication.translate("UTFWidget", "UTFWidget", None, QtGui.QApplication.UnicodeUTF8))
        self.menuUTF_test.setTitle(QtGui.QApplication.translate("UTFWidget", "UTF test", None, QtGui.QApplication.UnicodeUTF8))

class MyForm(QtGui.QMainWindow):
  def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)
    self.ui = Ui_UTFWidget()

if __name__ == "__main__":
  app = QtGui.QApplication(sys.argv)
  myapp = MyForm()

notice that there are two additem() methods, one which adds straight ASCII, the other which adds some non-ASCII characters. When I run the app, I see the first (abcde) in the list widget and don't see the second (áɬʔéí). No XML involved.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20120617/af1e45c4/attachment-0001.html>

More information about the PyQt mailing list