The transformation of these basic data types can sometimes make us a headache. Do not write, dare not write, write afraid of the wrong. Although now running QT down, this did not meet the first time a data type conversion is required.
First of all, the QT qstring how to turn char* it.
int main (int argc, char **argv)
{
Qapplication app (argc, argv);
QString str1 = "Test";
Qbytearray ba = Str1.tolatin1 ();
const char *C_STR2 = Ba.data ();
printf ("str2:%s", C_STR2);
return App.exec ();
}
Note here that the middle BA variable is essential and cannot be directly used with str1.tolatin1 (). Data ().
And then it's char* turn qstring:
Method One: QString string = QString (qlatin1string (C_STR2));
Method Two: If the Chinese system, directly with (const char*) str.local8bit ()
For example
printf ("%s", (const char*) str.local8bit ());
STR is a qstring
Recorded some of their own learning process of knowledge, hope to help you, also hope to reprint can retain the original address. Thanks.
Beyond C + + original articles, reproduced please indicate the source and keep the original link
This article link: http://www.beyondc.cn/qt-in-the-qstring-and-char-conversion.html