[Qt-interest] performance issue

jjDaNiMoTh jjdanimoth at gmail.com
Mon Jan 4 19:59:47 CET 2010


2010/1/4 João Abecasis <joao at trolltech.com>:
[cut]
>
> Specifically, loops such as,
>
>     QIODevice &device /* = getDevice() */;
>     QByteArray line;
>     while (!device.atEnd()) {
>         line = device.readLine();
>         // do something
>     }
>
> Can be changed to,
>
>     while (line = device.readLine(), !line.isEmpty()) {
>         // do something
>     }
>
> The line-break is included on "empty" lines, so that isEmpty will return
> false; line will be empty when the end of device is reached and there's
> nothing left to read.
>

No, it's wrong, because if you have a empty line on that file,
.isEmpty() will return true.
As described here [1],

QString("").isEmpty();          // returns true

Maybe you want use the method .isNull(), like described here [2]:

...If the stream has read to the end of the file, readLine() will
return a null QString

and an empty line will not reported [3]:

QString("").isNull();           // returns false


[1] http://doc.trolltech.com/4.6/qstring.html#isEmpty
[2] http://doc.trolltech.com/4.6/qtextstream.html#readLine
[3] http://doc.trolltech.com/4.6/qstring.html#isNull

hth,
-- 
Key Fingerprint
4588 F931 A6E7 C7D5 A302  3064 12EB E87F 9B28 A9D2




More information about the Qt-interest-old mailing list