In QT, the strings all use qstring, which is indeed convenient for developers. Think about the various variable types defined in VC, and the function parameter types are varied. This year's new type conversion is often required.
When QT uses third-party open-source libraries, the types of libraries are basically standard, and most of the string types are char *.
In QT, how to convert qstring to char * requires the qbytearray class. For details about the qbytearray class, see the QT help document.
Because char * finally has a '/0' as the Terminator, and when qstring: tolatin1 () is used,'/0' is added to the string'
The method is as follows:
Qstring STR;
Char * Ch;
Qbytearray BA = Str. tolatin1 ();
Ch = ba. Data ();
In this way, the conversion from qstring to char * is completed. TestedProgramNo bug occurs during running.
Note that the third line must be added. It cannot be completed by Str. tolatin1 (). Data (), and an error may occur.
Supplement: When qstring does not contain Chinese characters, no problem occurs. However, if qstring contains Chinese characters, conversion to char * is garbled. The following solution is used:
Method 1:
Added GBK encoding support:
# Include <qtextcodec>
Qtextcodec: setcodecfortr (qtextcodec: codecforname ("GBK "));
Qtextcodec: setcodecforlocale (qtextcodec: codecforname ("GBK "));
Then change the third behavior above: qbytearray BA = Str. toloacl8bit (); toloacl8bit supports Chinese
Method 2:
First convert qstring to the string type in the standard library, and then convert string to char *, as shown below:
STD: String STR = filename. tostdstring ();
Const char * Ch = Str. c_str ();