C++ Qt return empty QString

Normal People Scare Me picture Normal People Scare Me · May 4, 2013 · Viewed 12.2k times · Source

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?

Answer

Frank Osterfeld picture Frank Osterfeld · May 4, 2013

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.