<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><blockquote type="cite"><div>Message: 2<br>Date: Sun, 17 Jun 2012 18:42:54 +0200<br>From: Knacktus <<a href="mailto:knacktus@googlemail.com">knacktus@googlemail.com</a>><br>To: <a href="mailto:pyqt@riverbankcomputing.com">pyqt@riverbankcomputing.com</a><br>Subject: Re: [PyQt] including Unicode in QListWidget<br>Message-ID: <<a href="mailto:4FDE090E.1070905@googlemail.com">4FDE090E.1070905@googlemail.com</a>><br>Content-Type: text/plain; charset=ISO-8859-1; format=flowed<br><br>Am 17.06.2012 18:29, schrieb David Beck:<br><blockquote type="cite">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):<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">import sys<br></blockquote><blockquote type="cite">from PyQt4 import QtCore, QtGui<br></blockquote><blockquote type="cite">from xml.dom import minidom<br></blockquote><blockquote type="cite">import xml.etree.ElementTree as etree<br></blockquote><blockquote type="cite">from fieldbookGui import Ui_Fieldbook<br></blockquote><blockquote type="cite">import images<br></blockquote><blockquote type="cite">import btnCmds<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">class MyForm(QtGui.QMainWindow):<br></blockquote><blockquote type="cite">   def __init__(self, parent=None):<br></blockquote><blockquote type="cite">     QtGui.QWidget.__init__(self, parent)<br></blockquote><blockquote type="cite">     self.ui = Ui_Fieldbook()<br></blockquote><blockquote type="cite">     self.ui.setupUi(self)<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">     xmltree = etree.parse('BabyDb.xml')<br></blockquote><blockquote type="cite">     root = xmltree.getroot()<br></blockquote><blockquote type="cite">     for child in root:<br></blockquote><blockquote type="cite">       self.ui.hLexNav.addItem(child.findtext('Orth'))<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">The first 25 items that are returned by child.findtext('Orth') are:<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">['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?:"]<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">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.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Any ideas about what is going on would be appreciated.<br></blockquote><br>Are you 100 % sure that unicode is handled properly while reading the <br>xml? I never had problems with unicode and PyQt but I strictly using <br>unicode strings only in my apps.<br><br>This for example works for me (Python 2.7):<br><br># -*- coding: utf-8 -*-<br><br>if __name__ == "__main__":<br><br>     import sys<br>     from PyQt4.QtGui import *<br>     app = QApplication(sys.argv)<br>     list_widget = QListWidget()<br>     list_widget.addItem(u"??^?  l? l?")<br>     list_widget.show()<br>     app.exec_()<br><br></div></blockquote><br></div><div>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): </div><div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div>import sys</div></div><div><div>from PyQt4 import QtCore, QtGui</div></div><div><div><br></div></div><div><div>try:</div></div><div><div>    _fromUtf8 = QtCore.QString.fromUtf8</div></div><div><div>except AttributeError:</div></div><div><div>    _fromUtf8 = lambda s: s</div></div><div><div><br></div></div><div><div>class Ui_UTFWidget(object):</div></div><div><div>    def setupUi(self, UTFWidget):</div></div><div><div>        UTFWidget.setObjectName(_fromUtf8("UTFWidget"))</div></div><div><div>        UTFWidget.resize(400, 300)</div></div><div><div>        self.centralWidget = QtGui.QWidget(UTFWidget)</div></div><div><div>        self.centralWidget.setObjectName(_fromUtf8("centralWidget"))</div></div><div><div>        self.listWidget = QtGui.QListWidget(self.centralWidget)</div></div><div><div>        self.listWidget.setGeometry(QtCore.QRect(17, 9, 362, 241))</div></div><div><div>        self.listWidget.setObjectName(_fromUtf8("listWidget"))</div></div><div><div>        UTFWidget.setCentralWidget(self.centralWidget)</div></div><div><div>        self.menuBar = QtGui.QMenuBar(UTFWidget)</div></div><div><div>        self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 22))</div></div><div><div>        self.menuBar.setObjectName(_fromUtf8("menuBar"))</div></div><div><div>        self.menuUTF_test = QtGui.QMenu(self.menuBar)</div></div><div><div>        self.menuUTF_test.setObjectName(_fromUtf8("menuUTF_test"))</div></div><div><div>        UTFWidget.setMenuBar(self.menuBar)</div></div><div><div>        self.mainToolBar = QtGui.QToolBar(UTFWidget)</div></div><div><div>        self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))</div></div><div><div>        UTFWidget.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)</div></div><div><div>        self.statusBar = QtGui.QStatusBar(UTFWidget)</div></div><div><div>        self.statusBar.setObjectName(_fromUtf8("statusBar"))</div></div><div><div>        UTFWidget.setStatusBar(self.statusBar)</div></div><div><div>        self.menuBar.addAction(self.menuUTF_test.menuAction())</div></div><div><div><br></div></div><div><div>        self.retranslateUi(UTFWidget)</div></div><div><div>        QtCore.QMetaObject.connectSlotsByName(UTFWidget)</div></div><div><div><br></div></div><div><div>    def retranslateUi(self, UTFWidget):</div></div><div><div>        UTFWidget.setWindowTitle(QtGui.QApplication.translate("UTFWidget", "UTFWidget", None, QtGui.QApplication.UnicodeUTF8))</div></div><div><div>        self.menuUTF_test.setTitle(QtGui.QApplication.translate("UTFWidget", "UTF test", None, QtGui.QApplication.UnicodeUTF8))</div></div><div><div><br></div></div><div><div>class MyForm(QtGui.QMainWindow):</div></div><div><div>  def __init__(self, parent=None):</div></div><div><div>    QtGui.QWidget.__init__(self, parent)</div></div><div><div>    self.ui = Ui_UTFWidget()</div></div><div><div>    self.ui.setupUi(self)</div></div><div><div><br></div></div><div><div>    self.ui.listWidget.addItem("abcde")</div></div><div><div>    self.ui.listWidget.addItem("áɬʔéí")</div></div><div><div>    </div></div><div><div>if __name__ == "__main__":</div></div><div><div>  app = QtGui.QApplication(sys.argv)</div></div><div><div>  myapp = MyForm()</div></div><div><div>  myapp.show()</div></div><div><div>  sys.exit(app.exec_())</div></div></blockquote><div><div><br></div><div>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.</div><div><br></div></div></body></html>