33

我必须比较qt中的两个Qstrings,

说,

Qstring str1="1005",str2="1006";

我试过使用,

if(str1==str2){
   return true;
}

&

if(str1.compare(str2)==0)
{
    return true;
}

如果条件&返回true,两种方法仍然会进入内部。

4

3 回答 3

46

您可以使用 :

int x = QString::compare(str1, str2, Qt::CaseInsensitive);  // if strings are equal x should return 0
于 2013-10-09T06:41:38.083 回答
15

下面的代码对我来说很好。

int main(int argv, char **args)
 {
    QString str1="1005",str2="1006";
    if(str1 == str2)
        qDebug()<<"This should not print";
    qDebug()<<"Everything Ok";

}

输出:

Everything Ok

==运算符为 QStrings 重载,如此处所述

我不知道为什么你的代码不起作用。重新检查代码的其他部分。

于 2013-10-09T07:15:09.817 回答
-3

它在重建项目后工作,我认为这是 QT CREATOR 的问题

于 2013-10-09T07:28:53.710 回答