Using other encodings

As we've already mentioned, QString has convenient methods for decoding and encoding data in the most popular encodings, such as UTF-8, UTF-16, and Latin1. However, Qt knows how to handle many other encodings as well. You can access them using the QTextCodec class. For example, if you have a file in Big-5 encoding, you can ask Qt for a codec object by its name and make use of the fromUnicode() and toUnicode() methods:

QByteArray big5Encoded = big5EncodedFile.readAll();
QTextCodec *big5Codec = QTextCodec::codecForName("Big5");
QString text = big5Codec->toUnicode(big5Encoded);
QByteArray big5EncodedBack = big5Codec->fromUnicode(text); 
You can list the codecs supported on your installation using the QTextCodec::availableCodecs() ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.