c++qtqstring

How to return an empty QString


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?


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.