[Development] QString behavior change

Matthew Woehlke mwoehlke.floss at gmail.com
Thu Jul 30 18:14:45 CEST 2015


On 2015-07-30 03:38, Gerhard Scheikl wrote:
> The behavior of QString::trimmed has changed from 5.3.2 to 5.5.
> .trimmed() on an empty string (" ") makes it null
> .trimmed() on an empty string ("") doesn't make it null
> 
> Is this intended or a bug?

Whether or not it is¹ should not be relevant; QString has for a very
long time (since mid-4.x at least if not 4.0 or even longer) STRONGLY
discouraged differentiating between empty and null states. Don't do that
:-).

(¹ For a more literal answer, see Thiago's reply.)

> By the way: the output of qDebug is not as expected:
> there are additional whitespaces before true/false and there is even
> another one before null at the first "Before trim" output.
> 
> qDebug() << "String with space";
> QString my_string(" ");
> qDebug() << "Before trim: empty:" << my_string.isEmpty() << " null:" << my_string.isNull();
> my_string = my_string.trimmed();
> qDebug() << "After trim:  empty:" << my_string.isEmpty() << " null:" << my_string.isNull();
> 
> Output:
> 
> String with space
> Before trim: empty: false  null: false
> After trim:  empty: true  null: true

Your expectation that is wrong ;-). As noted, QDebug adds spaces between
"things", and you have a space in '" null:"', so the expected output is
two spaces between the previous item and "null"; one added automatically
by QDebug, and one that you printed explicitly.

-- 
Matthew




More information about the Development mailing list