Solved Conver QString to char
-
Hi,
One way to do that:
QByteArray utf8Data = myQString.toUtf8(); const char *string = utf8Data.constData();
-
What is the advantage compared to: toStdString().c_str(); ?
-
@Q139 toStdString().c_str(); Where i put the QString here xD
-
i'm trying with char not char *something
-
Char only hold 1 character, to hold more need multiple
QString qstr;
qstr.toStdString().c_str(); -
Sorry, so used to the
char *
use case that I didn't thought about that one.So something like
char myChar = myString[0].toLatin1()
? -
@SGaist thx a lot for your help. it's possible to convert char for utf16??
-
Not directly, utf-16 is at least 2 bytes, char is 1.
-
@SGaist Ok so for example i dont want put char like utf16 but do the conversion for example in my if(conver UTF16(char)) it's possible? thx a lot!
-
@Jeronimo said in Conver QString to char:
@SGaist thx a lot for your help. it's possible to convert char for utf16??
As @SGaist said, not with
char
. You'd need to use a wide char instead, such as:wchar_t myChar = myString[0].unicode();
However,
wchar_t
is compiler / platform dependant, and not portable when used in this way.What would you want to pass the UTF-16 character on to?
QChar::unicode()
would probably work for that case.Cheers.
-
@Paul-Colby said in Conver QString to char:
QChar::unicode()
solved i prefer this method:
QString cadena = "Hola";
std::string cadenaStd = cadena.toStdString();
QChar c = cadenaStd[0] or char -
Why not use
QChar c = cadena[0]
?