[Development] QString behavior change

Kobus Jaroslaw Jaroslaw.Kobus at theqtcompany.com
Fri Jul 31 09:46:07 CEST 2015


If you say not to differentiate empty and null states, some questions arise:
1) Why we have isNull and isEmpty at the same time?
2) Why their implementations are different?
3) What would be the advice on what to use in general: isNull of isEmpty?

Jarek

________________________________________
From: development-bounces+jaroslaw.kobus=theqtcompany.com at qt-project.org <development-bounces+jaroslaw.kobus=theqtcompany.com at qt-project.org> on behalf of Matthew Woehlke <mwoehlke.floss at gmail.com>
Sent: 30 July 2015 18:14
To: development at qt-project.org
Subject: Re: [Development] QString behavior change

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

_______________________________________________
Development mailing list
Development at qt-project.org
http://lists.qt-project.org/mailman/listinfo/development



More information about the Development mailing list