QTextCodec* codec = QTextCodec::codecForName("Windows-1251"); QString text = "Привет, мир!"; // hello world in Russian QByteArray encoded = codec->fromUnicode(text);
QFile file("example.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return; } QTextStream in(&file); in.setCodec("UTF-8"); QString text = in.readAll();In this example, we open a file named "example.txt" for reading text in UTF-8 encoding. We create a QTextStream object using the QFile object, and set the codec to UTF-8 using the setCodec() method. Finally, we read all the text from the file using the readAll() method. The QTextCodec class is part of the Qt Core module.