PDA

View Full Version : Please help with an "if statement" condition



ayanda83
27th August 2014, 16:38
Hi there, I want to compare two strings in an if statement condition as follows
if(meta->className() == "MyClass"){ //then do something} but the problem I am having is that meta->className() returns a string of type const char* which is incompatible to QString. Because of this, the "if" condition does not even evaluate instead it gives a warning - see attached picture10592

Radek
27th August 2014, 17:29
if(QString(meta->className()) == "MyClass"){ //then do something}

anda_skoa
28th August 2014, 09:18
but the problem I am having is that meta->className() returns a string of type const char* which is incompatible to QString.

The other operand is also const char*, there is no QString involved here.

Comparison of character arrays can be done via strcmp() functions such as http://qt-project.org/doc/qt-5/qbytearray.html#qstrncmp

Cheers,
_

wysota
28th August 2014, 09:45
Hi there, I want to compare two strings in an if statement condition as follows
if(meta->className() == "MyClass"){ //then do something} but the problem I am having is that meta->className() returns a string of type const char* which is incompatible to QString. Because of this, the "if" condition does not even evaluate instead it gives a warning - see attached picture10592

Qt4/Qt5 way:


if(meta->className() == QLatin1String("MyClass")) { ... }

Qt5 only way:

if(meta->className() == QStringLiteral("MyClass")) { ... }