Results 1 to 7 of 7

Thread: decoding from utf8

  1. #1

    Default decoding from utf8

    Hello,

    i am working with Qt4 and MySql. All the tables in my database have "DEFAULT CHARSET=utf8", because i am writing in Portuguese and i need accents, in my cpp file i use:

    Qt Code:
    1. res = QString::fromUtf8((const char*)r.data(), r.length());
    To copy to clipboard, switch view to plain text mode 

    and it works for most of the characters, but still i cant decode uppercase characters with accents like à or Ó.

    Does any one know why?

    Thanks in advance,

    Paulo

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: decoding from utf8

    what is "r"? a bytearray? how do you get the result from db to bytearry?

  3. #3

    Default Re: decoding from utf8

    i do not know if its the right way to do it but it have worked for me, in the code lines above the qr its the QSqlQuery and r is a string, i use it to apply the fromUtf8 function that use a const char*.

    Qt Code:
    1. QString s = qr.value(0).toString();
    2. string r = s.toStdString();
    3. res = QObject::fromUtf8((const char*)r.data());
    To copy to clipboard, switch view to plain text mode 

    For example:
    The result will be fine for the word "são" but it will not work for "SÃO".

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: decoding from utf8

    why cant you just directly use "s"? .. it will contain the right charecters... QString already utf..

    and r.data() return QChar and not char* so it is wrong..

  5. #5

    Default Re: decoding from utf8

    if i use,

    Qt Code:
    1. QString s = qr.value(0).toString();
    2. res = s;
    To copy to clipboard, switch view to plain text mode 

    the result for "SÃO GREGÓRIO" is "SÃO GREGÓRIO"

    if i use,
    Qt Code:
    1. QString s = qr.value(0).toString();
    2. res = QString::fromUtf8((const char*)s.data(), s.length());
    To copy to clipboard, switch view to plain text mode 

    the result for "SÃO GREGÓRIO" is "S" and 2 unprintable characters

    in any of this two examples it does not work for lowercase characters.

    if i use:
    Qt Code:
    1. QString s = qr.value(0).toString();
    2. string r = s.toStdString();
    3. res = QString::fromUtf8((const char*)r.data(), r.length());
    To copy to clipboard, switch view to plain text mode 

    the result for "SÃO GREGÓRIO" is "S�?O GREG�?RIO"

    and it works for lowercase characters (example: "são" results "são")

  6. #6
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: decoding from utf8

    Try:
    Qt Code:
    1. QByteArray b = qr.value(0).toByteArray();
    2. res = QString::fromUtf8(b.constData(), b.length());
    To copy to clipboard, switch view to plain text mode 

  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: decoding from utf8

    How about just:
    Qt Code:
    1. QString s = qr.value(0).toString();
    2. res = s;
    To copy to clipboard, switch view to plain text mode 

    Provided the appropriate column in the table is declared properly as a string column, Qt should automatically use the proper conversion.
    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.


Similar Threads

  1. UTF8 and QStandardItem?
    By alexandernst in forum Newbie
    Replies: 17
    Last Post: 26th July 2009, 18:29
  2. Download a web page to utf8.
    By Faster in forum Newbie
    Replies: 2
    Last Post: 13th April 2009, 20:49
  3. UTF8 to ISO 8859... conversion
    By claudio in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2007, 21:24
  4. Utf8 problems
    By cristiano in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2006, 00:14
  5. Detect utf8 text/html bool....
    By patrik08 in forum Newbie
    Replies: 2
    Last Post: 2nd June 2006, 11:39

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.