查看完整版本: [-- 中文问题!郁闷!求助 --]

QTCN开发网 -> Qt中文处理 -> 中文问题!郁闷!求助 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

hpplel 2018-07-20 17:14

中文问题!郁闷!求助

       QString fname="123大家好abc.docx";
       QString tmp=QString::fromLocal8Bit("123大家好abc.docx");
       if(fname.compare(tmp)==0) qDebug()<<"equal";


如上。中文系统。为什么两者不等啊?又是编码问题?哎,这个编码搞死我来,看了好多也没怎么弄懂!~

linker 2018-07-23 18:54
希望你可以多看看Qt的帮助文档。
1.
  1. One way to initialize a QString is simply to pass a const char * to its constructor. For example, the following code creates a QString of size 5 containing the data "Hello":


      QString str = "Hello";


    QString converts the const char * data into Unicode using the [backcolor=#ffff00]fromUtf8()[/backcolor] function.
2.
  1. [static] QString QString::fromLocal8Bit(const char *str, int size = -1)
    Returns a QString initialized with the first size characters of the 8-bit string str.
    If size is -1 (default), it is taken to be strlen(str).
    [backcolor=#ffff00]QTextCodec::codecForLocale() is used to perform the conversion.[/backcolor]
已上代码引用Qt帮助文档。
所以明白了吗?
QString str = "你好abc"; //使用的是utf8编码
QString str = QString::fromLocal8Bit("使用的是本地编码");

你可以这样:
  1. QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));


        QString str1 = "你好abc";
        QString str2 = QString::fromLocal8Bit("你好abc");


        qDebug() << (str1.compare(str2) == 0 ? "true" : "false");
这样是相等的。

Qt还是很不错的,既然想学,有问题了还是要学着看看帮助文档,不要怕英文,祝好。

hpplel 2018-07-24 08:24
多谢楼上!说得很明白了!~努力看帮助文档~!!!

九重水 2018-07-24 10:56
时到今日,为什么还有人往代码里直接写非ascii码字符,而不是采用翻译的方式了?

hpplel 2018-07-24 11:39
九重水:时到今日,为什么还有人往代码里直接写非ascii码字符,而不是采用翻译的方式了?[表情]
 (2018-07-24 10:56) 

小白请您详细说说!~

linker 2018-07-24 16:33
意思是用tr("")包起来,再使用翻译文件,可以避免中文编码导致的一系列问题


查看完整版本: [-- 中文问题!郁闷!求助 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled