QFile file("example.txt"); if (file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) { QTextStream stream(&file); stream.setCodec("UTF-8"); stream << "Hello, world!\n"; file.close(); }
QString text = "Привет, мир!"; QByteArray encoded = text.toUtf8(); QFile file("example.txt"); if (file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) { QTextStream stream(&file); stream.setCodec("UTF-8"); stream << encoded << "\n"; file.close(); }In this example, we create a QString containing the Russian text "Привет, мир!". We then convert this to a QByteArray using the UTF-8 encoding. We then write the encoded text to a file using a QTextStream object, which we have set the codec to be "UTF-8". Package/Library: Qt library.