Comparing QString with int value
-
Below is the code I wrote
QString aStr ='4'; if (aStr > 5) qDebug()<<"Condition-1"; else if (aStr<5) qDebug()<<"Condition-2"; else qDebug()<<"Condition-3";I took the following test cases for this code ;
CASE-1QString aStr='4';
CASE-2QString aStr='5';
CASE-3QString aStr='8';For all the above cases, I got same result as
"Condition-1".
Can someone explain me the reason and how to get the desired output? -
Below is the code I wrote
QString aStr ='4'; if (aStr > 5) qDebug()<<"Condition-1"; else if (aStr<5) qDebug()<<"Condition-2"; else qDebug()<<"Condition-3";I took the following test cases for this code ;
CASE-1QString aStr='4';
CASE-2QString aStr='5';
CASE-3QString aStr='8';For all the above cases, I got same result as
"Condition-1".
Can someone explain me the reason and how to get the desired output?@Swati777999 said in Comparing QString with int value:
Can someone explain me the reason
Something looks wrong. Are you certain that you have shown us your actual code? Your compiler should not allow you to compare a QString with an int.
and how to get the desired output?
Convert your string to an
intfirst. -
@Swati777999 said in Comparing QString with int value:
Can someone explain me the reason
Something looks wrong. Are you certain that you have shown us your actual code? Your compiler should not allow you to compare a QString with an int.
and how to get the desired output?
Convert your string to an
intfirst.If I had to guess, the operator overload
bool operator>(const char *other) constIs probably to blame. dont know what kind of warnings OP has enabled/disabled, and in its rawest form (5) could be implicitly cast to a pointer, I guess.
-
@Swati777999 said in Comparing QString with int value:
Can someone explain me the reason
Something looks wrong. Are you certain that you have shown us your actual code? Your compiler should not allow you to compare a QString with an int.
and how to get the desired output?
Convert your string to an
intfirst.yes, but how ? QString::toInt() is not working in this case for me.
-
yes, but how ? QString::toInt() is not working in this case for me.
@Swati777999 said in Comparing QString with int value:
QString::toInt() is not working in this case for me
Why not?
What happens? -
@Swati777999 said in Comparing QString with int value:
QString aStr ='4';
This doesn't compile for me, there is no QString constructor accepting a char.
If you want a plain-old-char-string then:QString aStr = "4";As expected, aStr.toInt() works just fine.
QString aStr = "4"; if (aStr.toInt() > 5) qDebug()<<"Condition-1"; else if (aStr.toInt() < 5) qDebug()<<"Condition-2"; else qDebug()<<"Condition-3"; qDebug() << aStr.toInt();Output:
Condition-2 4