zy_sunshine 发表于 2009-12-17 14:38:58

Qt QString QFile 与汉字问题

QFile cmdInfoTmp("cmdInfo.tmp");
    if (cmdInfoTmp.open(QIODevice::ReadOnly)) {
      QTextStream stream( &cmdInfoTmp );
      QString line;
      while ( !stream.atEnd() ) {
            line = stream.readLine();
            if(line.contains("成功创建")){
                QMessageBox::information(this, tr("info"), tr("have found the string"));
            }else{
                QMessageBox::information(this, tr("info"), tr("haven't' found the string"));
            }
      }
      cmdInfoTmp.close();
    }
    /*
    QString test("测试");
    if(test.contains("测")){
      QMessageBox::information(this, tr("info"), tr("haven found the string test"));
    }
    */
cmdInfo.tmp里面的内容如下:
项 {f2a062b0-ead1-11de-93dd-08002703cdb6} 成功创建。

就是从文件中读出的汉字在用QString 的contains判断的时候无效,有没有方法判断中文字符?或者我写的不对?但是下面的字符串测试正常啊。还是因为Qt的字符编码和win下的GB2312不是匹配的?

sejishikong 发表于 2009-12-17 15:37:47

你指定了程序的编码么?QTextCodec

zy_sunshine 发表于 2009-12-17 18:01:57

QTextCodec::setCodecForCStrings( QTextCodec::codecForName("GB2312") );
yes,这个解决问题

zy_sunshine 发表于 2009-12-19 23:10:31

stream.setCodec("ISO-8859-1");// default Winxp code Vista code
其实用这句才是真管用,不过用了之后输出的字符就成乱码了,不过能RegExp字符串了

sejishikong 发表于 2009-12-19 23:40:28

设置成这样的话,输出字符的时候需要再转换一次。

zy_sunshine 发表于 2009-12-20 00:36:04

恩,完美了。
不过Qt不能与cmd交互这点实在难以忍受。Qt不能获取cmd命令执行输出的字符串。

sejishikong 发表于 2009-12-20 00:43:06

当然能啊。
readAllStandardOutput

zy_sunshine 发表于 2009-12-20 01:00:50

p.waitForStarted();
      p.waitForFinished();

哦,我忘记用这两句去等待进程执行完毕,结果提前结束了cmd进程,没得到输出结果。现在好了,黑框框也可以去掉了。
就纳闷Qt这么nb的东西怎么会不支持这一点....原来我用错了

zy_sunshine 发表于 2009-12-20 01:01:22

QProcess p(0);
      p.start("route");
      p.waitForStarted();
      p.waitForFinished();
      //qDebug()<<QString::fromLocal8Bit(p.readAllStandardError());
      QMessageBox::information(this, tr("info"), p.readAllStandardError());

[ 本帖最后由 zy_sunshine 于 2009-12-20 01:04 编辑 ]

sejishikong 发表于 2009-12-20 01:25:23

呵呵,qt的确很好用的。不过如果是别的地方用,waitForFinished()应该加个默认超时,不然要是调用的程序僵死的话,会导致你的程序也僵死的。
页: [1]
查看完整版本: Qt QString QFile 与汉字问题