It turns out that automatic encoding detection is not possible for the general case.
However, there is a temporary solution to at least return to the system language if the text is invalid. Text UTF-8 / UTF-16 / UTF-32. It uses QTextCodec::codecForUtfText() , which attempts to decode the byte array using UTF-8, UTF-16, and UTF-32 and returns the default codec if it fails.
Code for this:
QTextCodec *codec = QTextCodec::codecForUtfText(byteArray, QTextCodec::codecForName("System")); const QString &text = codec->toUnicode(byteArray);
Update
The above code will not detect UTF-8 without a specification, however, since codecForUtfText () relies on specification markers. To detect UTF-8 without a specification, see fooobar.com/questions/541759 / ....
source share