View Full Version : decoding from utf8
pcaeiro
24th August 2009, 09:35
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:
res = QString::fromUtf8((const char*)r.data(), r.length());
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
nish
24th August 2009, 10:07
what is "r"? a bytearray? how do you get the result from db to bytearry?
pcaeiro
24th August 2009, 10:21
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*.
QString s = qr.value(0).toString();
string r = s.toStdString();
res = QObject::fromUtf8((const char*)r.data());
For example:
The result will be fine for the word "são" but it will not work for "SÃO".
nish
24th August 2009, 10:25
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..
pcaeiro
24th August 2009, 11:01
if i use,
QString s = qr.value(0).toString();
res = s;
the result for "SÃO GREGÓRIO" is "SÃO GREGÓRIO"
if i use,
QString s = qr.value(0).toString();
res = QString::fromUtf8((const char*)s.data(), s.length());
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:
QString s = qr.value(0).toString();
string r = s.toStdString();
res = QString::fromUtf8((const char*)r.data(), r.length());
the result for "SÃO GREGÓRIO" is "S�?O GREG�?RIO"
and it works for lowercase characters (example: "são" results "são")
numbat
25th August 2009, 10:11
Try:
QByteArray b = qr.value(0).toByteArray();
res = QString::fromUtf8(b.constData(), b.length());
wysota
25th August 2009, 12:51
How about just:
QString s = qr.value(0).toString();
res = s;
Provided the appropriate column in the table is declared properly as a string column, Qt should automatically use the proper conversion.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.