1 Attachment(s)
Please help with an "if statement" condition
Hi there, I want to compare two strings in an if statement condition as follows
Code:
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 pictureAttachment 10592
Re: Please help with an "if statement" condition
Code:
if(QString(meta
->className
()) == "MyClass"){ //then do something}
Re: Please help with an "if statement" condition
Quote:
Originally Posted by
ayanda83
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,
_
Re: Please help with an "if statement" condition
Quote:
Originally Posted by
ayanda83
Hi there, I want to compare two strings in an if statement condition as follows
Code:
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 picture
Attachment 10592
Qt4/Qt5 way:
Qt5 only way:
Code:
if(meta->className() == QStringLiteral("MyClass")) { ... }