All my source file are UTF-8 converted.
All my files Im opening are UTF-8.
My application is opening UTF-8 coded file which contains translated text for 3 languages: English, Polish and Russian and is saving the data to a file into 3 separate encoded blocks: Windows-1250 (English), Windows-1250 (Polish) and Windows-1251 (Russian) - yes that's right Im mixing encoding type inside one file which is then used by third-party device which know how to handle that.
Iv got a test program which worked flawlessly under Qt4 and now it stopped working (text is saved as ????????) when I moved to Qt5:
test_encoding.cpp
test_encoding::test_encoding(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); QString d; QFile f(QDir::currentPath() + "/input.txt"); if( f.open( QIODevice::ReadOnly | QIODevice::Text ) ) { d = f.readAll(); f.close(); } QFile ff(QDir::currentPath() + "/output.txt"); if( ff.open( QIODevice::WriteOnly | QIODevice::Text ) ) { QTextStream t(&ff); auto cutf8 = QTextCodec::codecForName("UTF-8"); auto cw50 = QTextCodec::codecForName("windows-1250"); auto cw51 = QTextCodec::codecForName("windows-1251"); // ____Block 1 t.setCodec(cutf8); t << d << "\r\n"; t << cutf8->fromUnicode(d) << "\r\n"; t.flush(); // ____Block 2 t.setCodec(cw50); t << d << "\r\n"; t << cw50->fromUnicode(d) << "\r\n"; t.flush(); // ____Block 3 t.setCodec(cw51); t << d << "\r\n"; t << cw51->fromUnicode(d) << "\r\n"; t.flush(); } ff.close(); QCoreApplication::quit(); }
input.txt (UTF-8 without BOM)
Użytkownik niezalogowany
Not logged-in user
Не зарегистрированный
- output.txt (multi code page blocks)
____Block 1:
Użytkownik niezalogowany
Not logged-in user
Не зарегистрированный
Użytkownik niezalogowany
Not logged-in user
Не зарегистрированный
____Block 2:
U࠹tkownik niezalogowany
Not logged-in user
?? ??????????????????
U?ytkownik niezalogowany
Not logged-in user
?? ??????????????????
____Block 3:
U࠹tkownik niezalogowany
Not logged-in user
?? ??????????????????
U?ytkownik niezalogowany
Not logged-in user
?? ??????????????????
It appears it is possible to save the text only to UTF-8 which is not suitable for me - i need to use code pages Windows-1251 and Windows-1250.
Is it possible in Qt5 to convert from UTF-8 to other code pages?