Results 1 to 18 of 18

Thread: UTF8 and QStandardItem?

  1. #1
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default UTF8 and QStandardItem?

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UTF8 and QStandardItem?

    QString::fromUtf8()
    Qt Code:
    1. QStandardItem(QString::fromUtf8("utf-8 encoded text"));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: UTF8 and QStandardItem?

    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/...l/qstring.html

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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UTF8 and QStandardItem?

    Quote Originally Posted by alexandernst View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: UTF8 and QStandardItem?

    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.

  6. #6
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: UTF8 and QStandardItem?

    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 (¿?)

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UTF8 and QStandardItem?

    How is contact.name defined? How did data end up in it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: UTF8 and QStandardItem?

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UTF8 and QStandardItem?

    I'm not asking about the content, I'm asking about the object class itself. What does type() return on contact.name?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: UTF8 and QStandardItem?

    It's an instance.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UTF8 and QStandardItem?

    Instance of what?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: UTF8 and QStandardItem?


  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UTF8 and QStandardItem?

    contact.name is an instance of ContactView? What did you want to obtain by putting it into a QStandardItem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: UTF8 and QStandardItem?

    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) )
    Last edited by alexandernst; 26th July 2009 at 15:14.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UTF8 and QStandardItem?

    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().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: UTF8 and QStandardItem?

    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:

    Qt Code:
    1. self._model.set_value(citer, 2, common.escape_pango(str(contactview.name)))
    To copy to clipboard, switch view to plain text mode 

    and escape_pango is like this:

    Qt Code:
    1. def escape_pango(str):
    2. str = gobject.markup_escape_text(str)
    3. str = str.replace('\n',' ')
    4. return str
    To copy to clipboard, switch view to plain text mode 

    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?

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UTF8 and QStandardItem?

    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?
    python Code:
    1. QStandardItem(QString.fromUtf8(str(contact.name)));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: UTF8 and QStandardItem?

    Woow !! I think that's the only combination of "from*" and "str" that I didn't tried, and it works! Thank you wysota

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.