我正在使用QT Creator,创建了一个控制台应用程序.一切都是最新的.操作系统是Windows XP.
我创建了一个包含匈牙利字符的QString.大多数匈牙利字符不需要unicode,但是带有双斜线的字符需要unicode.
我尝试将QString内容写入文件,但我的unicode字符在文件中丢失了重音符号.换句话说,unicode信息在此过程中丢失了.
我的代码是吼叫.
#include <QtCore/QCoreApplication>
#include <QString>
#include <QTextStream>
#include <QDate>
#include <QFile>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QString szqLine = "NON-UnicodeIsOK: áéüúöóí NEED-Unicode: ??";
//These are Hungarian chars and require unicode. Actually, only the u & o, each having double
//slashes for eccents require unicode encoding.
//Open file for writing unicode chars to.
QFile file("out.txt");
if ( !file.open(QIODevice::WriteOnly | QIODevice::Text) ){
return 1;
}
//Stream the QString text to the file.
QTextStream out(&file);
out.setCodec("UTF-8");
out << szqLine << endl; //Use endl for flush. Does not properly write ? and ? chars.
//Accents missing in file.
file.close(); //Done with file.
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
您的文件的编码是什么?在源文件中使用非ascii编码通常会导致问题,至少在跨平台工作时会出现问题.我认为MSVC存在一些问题.
QString foo ="unicode string"使用从ascii到unicode的隐式转换,这也会导致问题.总是明确指定文字使用的编码,例如,如果它是latin1,则使用QLatin1String()包装文字:
QString foo = QLatin1String("some latin1 string");
或者,utf-8,就像你的情况一样,QString :: fromUtf8():
QString foo = QString::fromUtf8( "funny characters" );
Run Code Online (Sandbox Code Playgroud)
为了避免这样的错误,我更喜欢使用英文字符串保持源文件纯ascii,然后使用Qt的国际化工具进行翻译.
编辑:另请参阅MSVC 2008中有关UTF-8文字的此问题的已接受答案.
| 归档时间: |
|
| 查看次数: |
5024 次 |
| 最近记录: |