QString to char* conversion
-
@JonB said in QString to char* conversion:
@Emre-MUTLU
Yep, I agree,toLocal8Bit()
seems to be the way to go. Maybe that's allstd::string
does anyway?std::string is encoding agnostic, so the encoding is the one you choose to use.
ISO-8859 tried to be a "de facto standard" at least on windows and linux, but apple used MacRoman.
Before unicode/utf8 transcoding between OS was pure nightmare.@mpergand said in QString to char* conversion:
std::string is encoding agnostic, so the encoding is the one you choose to use.
And Qt defines that every std::string created from QString is UTF-8 encoded:
inline std::string QString::toStdString() const { return toUtf8().toStdString(); }
Even QString::toLocal8Bit() on Linux assumes the locale is UTF-8 - encoded without looking at the real locale, on Window the locale is respected.
-
const char* ch
will compile, but you can't use that afterwards.toStdString
creates a temporary, which is destroysed after;
, so your variable points to released memory.If you want to use it in some function that takes a
const char*
as parameter you can do it either like this:SomeFunction(text.toStdString().c_str()); //temporary is in scope still
or like this:
std::string s = text.toStdString(); SomeFunction(s.c_str());
but you can't do this:
const char* ch = text.toStdString().c_str(); //ch is pointing to garbage at this point SomeFunction(ch);
@Chris-Kawa
I would like to passQString
topszTxt
parameter in this function:
fun1(HANDLE hDocument, PSTR pszTxt)
Below doesn't works:
void C56SdkApp::PrintTest(QString text) { char* ch = text.toUtf8().toStdString(); fun1(hDocument, ch ); }
-
@Damian7546 Well it doesn't work because you're assigning std::string to char* variable. It doesn't make any sense.
You have to check what encoding the function fun1 expects in its pszTxt parameter.
If its local codepage thenfun1(hDocument, text.toLocal8Bit().constData());
If it's UTF-8 then
fun1(hDocument, text.toUtf8().constData());
-
@JonB said in QString to char* conversion:
I wish the docs said so!!!!
As soon as an object is returned from any function (even a plain C function) it's a temporary until you assign it to a local variable.
@Christian-Ehrlicher said in QString to char* conversion:
As soon as an object is returned from any function (even a plain C function) it's a temporary until you assign it to a local variable.
any ? what if I returned a static object ?
-
@Christian-Ehrlicher said in QString to char* conversion:
As soon as an object is returned from any function (even a plain C function) it's a temporary until you assign it to a local variable.
any ? what if I returned a static object ?
@J-Hilk said in QString to char* conversion:
any ? what if I returned a static object ?
If you return it by value it's still a temporary (copy), unless you return a reference. But even if you do it's technically still a temporary. It's just that the reference is a temporary, not the object it references. To simplify - anything that doesn't have a name is a temporary.
-
@Damian7546 Well it doesn't work because you're assigning std::string to char* variable. It doesn't make any sense.
You have to check what encoding the function fun1 expects in its pszTxt parameter.
If its local codepage thenfun1(hDocument, text.toLocal8Bit().constData());
If it's UTF-8 then
fun1(hDocument, text.toUtf8().constData());
@Chris-Kawa said in QString to char* conversion:
@Damian7546 Well it doesn't work because you're assigning std::string to char* variable. It doesn't make any sense.
You have to check what encoding the function fun1 expects in its pszTxt parameter.
If its local codepage then
fun1(hDocument, text.toLocal8Bit().constData());If it's UTF-8 then
fun1(hDocument, text.toUtf8().constData());But in this way doesn't works. ->
No matching fuction for call to fun1
-
@Chris-Kawa said in QString to char* conversion:
@Damian7546 Well it doesn't work because you're assigning std::string to char* variable. It doesn't make any sense.
You have to check what encoding the function fun1 expects in its pszTxt parameter.
If its local codepage then
fun1(hDocument, text.toLocal8Bit().constData());If it's UTF-8 then
fun1(hDocument, text.toUtf8().constData());But in this way doesn't works. ->
No matching fuction for call to fun1
@Damian7546 said in QString to char* conversion:
No matching fuction for call to fun1
Then you should show the signature of this function....
-
@Chris-Kawa said in QString to char* conversion:
@Damian7546 Well it doesn't work because you're assigning std::string to char* variable. It doesn't make any sense.
You have to check what encoding the function fun1 expects in its pszTxt parameter.
If its local codepage then
fun1(hDocument, text.toLocal8Bit().constData());If it's UTF-8 then
fun1(hDocument, text.toUtf8().constData());But in this way doesn't works. ->
No matching fuction for call to fun1
@Damian7546 Ugh, right, the function takes PSTR, which is a non-const pointer. It's not nice of it, but it just means you have to give it a non-const pointer i.e.
fun1(hDocument, text.toLocal8Bit().data());
or
fun1(hDocument, text.toUtf8().data());
Just keep in mind that since it takes a non-const pointer it indicates that it can change the content of that string, which, if it does, will be lost, since it's a temporary.
-
@Damian7546 Ugh, right, the function takes PSTR, which is a non-const pointer. It's not nice of it, but it just means you have to give it a non-const pointer i.e.
fun1(hDocument, text.toLocal8Bit().data());
or
fun1(hDocument, text.toUtf8().data());
Just keep in mind that since it takes a non-const pointer it indicates that it can change the content of that string, which, if it does, will be lost, since it's a temporary.
@Chris-Kawa said in QString to char* conversion:
, if it does, will be lost, since it's a temporary.
And more than that - it will crash when the new string is longer than the actual one.
You really should read the API documentation for this instead doing try & error...
-
@Damian7546 Ugh, right, the function takes PSTR, which is a non-const pointer. It's not nice of it, but it just means you have to give it a non-const pointer i.e.
fun1(hDocument, text.toLocal8Bit().data());
or
fun1(hDocument, text.toUtf8().data());
Just keep in mind that since it takes a non-const pointer it indicates that it can change the content of that string, which, if it does, will be lost, since it's a temporary.
@Chris-Kawa Thank you very much. It works.