During QT development, I often do not hesitate to convert the qstring to the char * type through toascii (). Data (), So I wasted more time searching for bugs.
Case:
In a project, you need to pass a long string, but use toascii (). the char * pointer obtained by data () conversion. During the running process, the pointer is often changed to garbled characters at a certain position, so the string is truncated, which leaves me confused several times.
Later, Baidu changed toascii (). Data () to tolatin1 (). Data (). The problem was solved.
In Main. CPP, the following Chinese encoding is often changed:
#include <QTextCodec>QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
Therefore, if the qstring contains Chinese characters, use toascii () conversion. If only English characters exist, use tolatin1.
The QT document says:
Qbytearray qstring: toascii () const
Returns an 8-bit representation of the string as a qbytearray.
If a codec has been set using qtextcodec: setcodecforcstrings (), it is used to convert Unicode to 8-bit char; otherwise this function does the same as tolatin1 ().
Note that, despite the name, this function does not necessarily return an US-ASCII (ANSI X3.4-1986) string and its result may not be US-ASCII compatible.
If the setcodecforcstrings function is used for encoding, toascii converts Unicode to an 8-bit char type. Otherwise, it is the same as tolatin1.