我的所有源文件都经过UTF-8转换。
我打开的所有文件都是UTF-8编码的。
我的应用程序打开了一个UTF-8编码的文件,其中包含了三种语言(英语、波兰语和俄语)的翻译文本,并将数据保存到一个文件中,分为三个单独的编码块:Windows-1250(英语)、Windows-1250(波兰语)和Windows-1251(俄语) - 是的,我在一个文件内混合使用不同的编码类型,这个文件由第三方设备使用,该设备知道如何处理它。
我有一个测试程序,在Qt4下完美运行,但当我移植到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)
未登录用户
Not logged-in user
未注册用户
- output.txt (多代码页块)
____块1:
未登录用户
Not logged-in user
未注册用户
未登录用户
Not logged-in user
未注册用户
____块2:
未登录用户
Not logged-in user
无法识别的字符集
未登录用户
Not logged-in user
无法识别的字符集
____块3:
未登录用户
Not logged-in user
无法识别的字符集
未登录用户
Not logged-in user
无法识别的字符集
看起来只能将文本保存为UTF-8,这对我来说不太合适 - 我需要使用Windows-1251和Windows-1250代码页。
在Qt5中是否可以将UTF-8转换为其他代码页?