PDA

View Full Version : UTF8 and QStandardItem?



alexandernst
26th July 2009, 00:38
Hi, I have some little trouble: how can I setText to a QStandardItem having a utf8 encoded string? I mean, something like:

string = "ñññññ" # <-- this is utf8 (just for the example)
item = QStandardItemModel()
item.setText(string) # <-- this wont work! It will add the string, but wont be decoded. It'll be just like \xz1\xz1....

I tried using fromUtf8() without luck. Maybe I did it wrong?

Thanks

wysota
26th July 2009, 00:56
QString::fromUtf8()


QStandardItem(QString::fromUtf8("utf-8 encoded text"));

alexandernst
26th July 2009, 01:17
TypeError: argument 1 of QString.fromUtf8() has an invalid type

I suppose that this error means that contact.name isn't a utf-8 encoded string. What about unicode? How can I setText with an unicode string? There isn't any fromUnicode method, http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qstring.html

If the string isn't unicode encoded, how could I know how is encoded?

wysota
26th July 2009, 01:22
I suppose that this error means that contact.name isn't a utf-8 encoded string.
I don't think so. More like it's not a string at all.

Can we see the actual code?

alexandernst
26th July 2009, 01:26
Sure :)

giter.child(self._model.indexFromItem(citer).row() , 2).setText(QString.fromUtf8(contact.name))

That the line. giter is the parent if the QStandrdItem (the child is QStandardItem too). I try to add the text in the 3rd column (index 2). The contact.name is passed to the def.

alexandernst
26th July 2009, 02:17
I have to say that

giter.child(self._model.indexFromItem(citer).row() , 2).setText(QString.fromUtf8(str(contact.name)))

will add the text, so the problem is the encoding (¿?)

wysota
26th July 2009, 09:24
How is contact.name defined? How did data end up in it?

alexandernst
26th July 2009, 12:28
I have been reading since yesterday, and I think I'm really confused. The answer of "what is contact" and "how is contact.name defined" is not that easy.

contact.name is a strigview, defined in amsn2's <class 'amsn2.core.views.stringview.StringView'>

edit: so, stringview is actualy an html formatted string.

wysota
26th July 2009, 12:39
I'm not asking about the content, I'm asking about the object class itself. What does type() return on contact.name?

alexandernst
26th July 2009, 12:42
It's an instance.

wysota
26th July 2009, 13:52
Instance of what?

alexandernst
26th July 2009, 15:56
Instance of a contactview http://github.com/drf/amsn2/blob/50705692cbab5dc71665eb1afe61e4b6b84a809e/amsn2/core/views/contactlistview.py

wysota
26th July 2009, 16:04
contact.name is an instance of ContactView? What did you want to obtain by putting it into a QStandardItem?

alexandernst
26th July 2009, 16:07
No. contact is an instante of contactview. contact.name contains an html formatted text. I wan't to put that text into a QStandardItem.

edit: Maybe I dont explain myself well (sorry for my english)

contactview is an instance of contactview.

contact.name is an <class 'amsn2.core.views.stringview.StringView'>
(that's what I get from type(contact.name) )

wysota
26th July 2009, 18:00
So that's a StringView. You need to find a way to convert StringView to a string, then you can pass that to QString::fromUtf8().

alexandernst
26th July 2009, 18:28
QString(str(contact.name)) will add this to the QStandardItem, but I wont get the non-ascii symbols. Instead of that, I'll get... weard letters from the ascii table.

I have been looking the other front-end (the gtk one) (I'm working on the qt4 one) and the string there is assigned this way:


self._model.set_value(citer, 2, common.escape_pango(str(contactview.name)))

and escape_pango is like this:



def escape_pango(str):
str = gobject.markup_escape_text(str)
str = str.replace('\n',' ')
return str


So, I need to parse the html formatted contact.name string. What could I use? cgi.escape? Could you give me an example if that's the right one, please?

wysota
26th July 2009, 19:23
GTK doesn't use Unicode, so a conversion is not required. If you are getting "weird" characters here, then a conversion from utf-8 (or other appropriate encoding) is required.

Did you try this?
QStandardItem(QString.fromUtf8(str(contact.name))) ;

alexandernst
26th July 2009, 19:29
Woow !! I think that's the only combination of "from*" and "str" that I didn't tried, and it works! Thank you wysota ;)