Question

I made a function which returns a QString. At some points in my function it should return an empty QString.

Just returning "" doesn't work. When I use QString::isEmpty() it's not. My "emergency plan" was to return an "empty" string and check with it whether the text is "empty". But I don't think that's good style.

So how do I return an empty QString?

Was it helpful?

Solution

The idiomatic way to create an empty QString is using its default constructor, i.e. QString(). QString() creates a string for which both isEmpty() and isNull() return true.

A QString created using the literal "" is empty (isEmpty() returns true) but not null (isNull() returns false).

Both have a size()/length() of 0.

OTHER TIPS

According to the QString docs, returning "" should work; as should returning QString().

Try printing out the string you are testing to make sure it's really empty:

printf("xxx [%s]\n", myStr.toUtf8().constData());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top