<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><blockquote type="cite"><div><br>Message: 1<br>Date: Mon, 18 Jun 2012 05:03:28 +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:4FDE9A80.50100@googlemail.com">4FDE9A80.50100@googlemail.com</a>><br>Content-Type: text/plain; charset=UTF-8; format=flowed<br><br>Am 17.06.2012 22:55, schrieb David Beck:<br><blockquote type="cite"><blockquote type="cite">Message: 2<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">Date: Sun, 17 Jun 2012 18:42:54 +0200<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">From: Knacktus <<a href="mailto:knacktus@googlemail.com">knacktus@googlemail.com</a> <<a href="mailto:knacktus@googlemail.com">mailto:knacktus@googlemail.com</a>>><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">To: <a href="mailto:pyqt@riverbankcomputing.com">pyqt@riverbankcomputing.com</a> <<a href="mailto:pyqt@riverbankcomputing.com">mailto:pyqt@riverbankcomputing.com</a>><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">Subject: Re: [PyQt] including Unicode in QListWidget<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">Message-ID: <<a href="mailto:4FDE090E.1070905@googlemail.com">4FDE090E.1070905@googlemail.com</a><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><<a href="mailto:4FDE090E.1070905@googlemail.com">mailto:4FDE090E.1070905@googlemail.com</a>>><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">Content-Type: text/plain; charset=ISO-8859-1; format=flowed<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">Am 17.06.2012 18:29, schrieb David Beck:<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">I am trying to build a GUI for navigating through a large XML<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">database on a Mac running OS 10.7, Python 3.3, PyQt 4. I want to get<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">a list of the text in all of the nodes called<Orth> and put them into<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">a QListWidget called "hLexNav". To do this, I wrote the following bit<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">of code (this isn't the whole thing, just the parts that are supposed<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">to add items to the listbox):<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">import sys<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">from PyQt4 import QtCore, QtGui<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">from xml.dom import minidom<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">import xml.etree.ElementTree as etree<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">from fieldbookGui import Ui_Fieldbook<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">import images<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">import btnCmds<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">class MyForm(QtGui.QMainWindow):<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">def __init__(self, parent=None):<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">QtGui.QWidget.__init__(self, parent)<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">self.ui = Ui_Fieldbook()<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">self.ui.setupUi(self)<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">xmltree = etree.parse('BabyDb.xml')<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">root = xmltree.getroot()<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">for child in root:<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">self.ui.hLexNav.addItem(child.findtext('Orth'))<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">The first 25 items that are returned by child.findtext('Orth') are:<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">['a:', 'a:ch?j', 'a:chul?:', "a:h?:xtu'", 'a:ho:t?n', 'a:k?s',<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">"a:li:ma'ht?n", 'a:li:st?:n', 'a:m?', "a:ma'ha:'pi'tz?'n",<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">'a:mixtzay?n', 'a:nan?:', 'a:t?:n', 'a:tz?:', "a:tzem?'j", 'a:x?:lh',<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">'a:xt?m', 'a:x?:x', "a:'h?la'", "a:'j", "a:'jm?", "a:'jnan?:",<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">"a:'jtz?:", "a:'jtzanan?:", "a:'kn?:"]<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">In the QListWidget created by this code, I see only items<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">corresponding to those elements that do not contain accented vowels<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">(here, those that don't contain "?", "?", etc.); items that<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">correpsond to strings with accented vowels are left empty. Further<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">experimentation with addItem( ), addItems(), and insertItem( ) show<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">that any string that contains an non-ASCII character results in an<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">empty Item being inserted into the QListWidget.<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite">Any ideas about what is going on would be appreciated.<br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">Are you 100 % sure that unicode is handled properly while reading the<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">xml? I never had problems with unicode and PyQt but I strictly using<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">unicode strings only in my apps.<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">This for example works for me (Python 2.7):<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"># -*- coding: utf-8 -*-<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">if __name__ == "__main__":<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">import sys<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">from PyQt4.QtGui import *<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">app = QApplication(sys.argv)<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">list_widget = QListWidget()<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">list_widget.addItem(u"??^? l? l?")<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">list_widget.show()<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">app.exec_()<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Yes, it seems to be independent of the XML. For instance, I get the same<br></blockquote><blockquote type="cite">thing when I run the little app below (the GUI is generated by pyuic4):<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"><br></blockquote><blockquote type="cite">    try:<br></blockquote><blockquote type="cite">    _fromUtf8 = QtCore.QString.fromUtf8<br></blockquote><blockquote type="cite">    except AttributeError:<br></blockquote><blockquote type="cite">    _fromUtf8 = lambda s: s<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">    class Ui_UTFWidget(object):<br></blockquote><blockquote type="cite">    def setupUi(self, UTFWidget):<br></blockquote><blockquote type="cite">    UTFWidget.setObjectName(_fromUtf8("UTFWidget"))<br></blockquote><blockquote type="cite">    UTFWidget.resize(400, 300)<br></blockquote><blockquote type="cite">    self.centralWidget = QtGui.QWidget(UTFWidget)<br></blockquote><blockquote type="cite">    self.centralWidget.setObjectName(_fromUtf8("centralWidget"))<br></blockquote><blockquote type="cite">    self.listWidget = QtGui.QListWidget(self.centralWidget)<br></blockquote><blockquote type="cite">    self.listWidget.setGeometry(QtCore.QRect(17, 9, 362, 241))<br></blockquote><blockquote type="cite">    self.listWidget.setObjectName(_fromUtf8("listWidget"))<br></blockquote><blockquote type="cite">    UTFWidget.setCentralWidget(self.centralWidget)<br></blockquote><blockquote type="cite">    self.menuBar = QtGui.QMenuBar(UTFWidget)<br></blockquote><blockquote type="cite">    self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 22))<br></blockquote><blockquote type="cite">    self.menuBar.setObjectName(_fromUtf8("menuBar"))<br></blockquote><blockquote type="cite">    self.menuUTF_test = QtGui.QMenu(self.menuBar)<br></blockquote><blockquote type="cite">    self.menuUTF_test.setObjectName(_fromUtf8("menuUTF_test"))<br></blockquote><blockquote type="cite">    UTFWidget.setMenuBar(self.menuBar)<br></blockquote><blockquote type="cite">    self.mainToolBar = QtGui.QToolBar(UTFWidget)<br></blockquote><blockquote type="cite">    self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))<br></blockquote><blockquote type="cite">    UTFWidget.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)<br></blockquote><blockquote type="cite">    self.statusBar = QtGui.QStatusBar(UTFWidget)<br></blockquote><blockquote type="cite">    self.statusBar.setObjectName(_fromUtf8("statusBar"))<br></blockquote><blockquote type="cite">    UTFWidget.setStatusBar(self.statusBar)<br></blockquote><blockquote type="cite">    self.menuBar.addAction(self.menuUTF_test.menuAction())<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">    self.retranslateUi(UTFWidget)<br></blockquote><blockquote type="cite">    QtCore.QMetaObject.connectSlotsByName(UTFWidget)<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">    def retranslateUi(self, UTFWidget):<br></blockquote><blockquote type="cite">    UTFWidget.setWindowTitle(QtGui.QApplication.translate("UTFWidget",<br></blockquote><blockquote type="cite">    "UTFWidget", None, QtGui.QApplication.UnicodeUTF8))<br></blockquote><blockquote type="cite">    self.menuUTF_test.setTitle(QtGui.QApplication.translate("UTFWidget",<br></blockquote><blockquote type="cite">    "UTF test", None, QtGui.QApplication.UnicodeUTF8))<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_UTFWidget()<br></blockquote><blockquote type="cite">    self.ui.setupUi(self)<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">    self.ui.listWidget.addItem("abcde")<br></blockquote><blockquote type="cite">    self.ui.listWidget.addItem("?????")<br></blockquote><br>Make this a unicode string (Python 2.7):<br><br>self.ui.listWidget.addItem(u"?????")<br><br><blockquote type="cite">    if __name__ == "__main__":<br></blockquote><blockquote type="cite">    app = QtGui.QApplication(sys.argv)<br></blockquote><blockquote type="cite">    myapp = MyForm()<br></blockquote><blockquote type="cite">    myapp.show()<br></blockquote><blockquote type="cite">    sys.exit(app.exec_())<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">notice that there are two additem() methods, one which adds straight<br></blockquote><blockquote type="cite">ASCII, the other which adds some non-ASCII characters. When I run the<br></blockquote><blockquote type="cite">app, I see the first (abcde) in the list widget and don't see the second<br></blockquote><blockquote type="cite">(?????). No XML involved.<br></blockquote><br>Works for me with above modification and declaring the source file <br>format as utf-8 (Python 2.7).<br><br><font class="Apple-style-span" color="#05731c"><br></font></div></blockquote></div>Thanks, but moving back to Python 2.7 is a last resort. I will be working with too much non-ASCII text and really wanted to use Python 3 so I didn't have to worry about these encoding issues. I don't want to switch unless there's no alternative.</body></html>