Can anyone tell me how to convert QString to const char*?
for example
QString mystring;
I tried
mystring.data(); mystring.toLatin1(); mystring.toAscii();
none of them work.![]()
Can anyone tell me how to convert QString to const char*?
for example
QString mystring;
I tried
mystring.data(); mystring.toLatin1(); mystring.toAscii();
none of them work.![]()
I just tried:
Qt Code:
const char *c = a.toAscii();To copy to clipboard, switch view to plain text mode
It seems to work.
You can use qPrintable macro.
They all work, but they are not designed to give you a char* (const or otherwise). You want to read the relevant parts of the QByteArray docs and:
Qt Code:
char *str1 = mystring.toLatin1().data(); char *str2 = mystring.toAscii().data();To copy to clipboard, switch view to plain text mode
simply use
QString l_sStr = "Hello";
const char* test = l_sStr.toLatin1().data(); // in Qt4
const char* test = l_sStr.tolatin1();//in Qt3l
Most of what is said in this thread is wrong, guys. I'll give you a hint - storing a pointer to data from a temporary object may lead to a crash.
Been there, done that, got the T-Shirt. Watched the application create a temporary QByteArray, grab the pointer to the constant data of the QByteArray and then the QByteArray was destroyed before the next line of code was executed, thus the pointer was no longer valid. Random crashes and garbage data from that point on. Now I'm very care about such things.
Bookmarks