Question

I have to compare two Qstrings in qt,

say,

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

I have tried using ,

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

&

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

still both methods goes inside if condition & returns true.

Was it helpful?

Solution 3

It worked after Rebuilding the Project , I think this is the problem with QT CREATOR

OTHER TIPS

You can use :

int x = QString::compare(str1, str2, Qt::CaseInsensitive);  // if strings are equal x should return 0

The code below works fine for me.

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

}

Output:

Everything Ok

The == operator is overloaded for QStrings, as documented here.

I don't know why your code is not working. Recheck other parts of your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top