Am 17.06.2012 22:55, schrieb David Beck:
Message: 2
Date: Sun, 17 Jun 2012 18:42:54 +0200
From: Knacktus <knack...@googlemail.com <mailto:knack...@googlemail.com>>
To: pyqt@riverbankcomputing.com <mailto:pyqt@riverbankcomputing.com>
Subject: Re: [PyQt] including Unicode in QListWidget
Message-ID: <4fde090e.1070...@googlemail.com
<mailto:4fde090e.1070...@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

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

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

    self.retranslateUi(UTFWidget)
    QtCore.QMetaObject.connectSlotsByName(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()
    self.ui.setupUi(self)

    self.ui.listWidget.addItem("abcde")
    self.ui.listWidget.addItem("áɬʔéí")

Make this a unicode string (Python 2.7):

self.ui.listWidget.addItem(u"áɬʔéí")

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


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.

Works for me with above modification and declaring the source file format as utf-8 (Python 2.7).







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

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

Reply via email to