Back to Blog home

Buffered QTextStream + pos() == ?

Published on Saturday June 17, 2006 by Andreas Aardal Hanssen in Uncategorized | Comments

[Disclaimer: Qt 4.2 is not released yet, and anything can still change.]

QTextStream in Qt 3 was unbuffered. Feeding bytes by bytes into a codec which appended QChar by QChar into a QString, it did have room for some speed improvements. So in Qt 4.0, we introduced QTextStream's replacement: ... QTextStream! That is - we completely refactored its insides, but kept the API more or less the same. The new QTextStream is buffered. That gave QTextStream in Qt 4 a 16x performance boost over Qt 3. For most operations, it's just about 1.5x slower than raw QFile access. Which is very very good.

The downside from this change was that the stream's device position, which can be retrieved by accessing QTextStream::device()->pos(), became unreliable. Because QTextStream buffers its input, it reads ahead a chunk of bytes. In Qt 3, QTextStream was always in sync with the device position. In Qt 4, the device is way ahead! And this upset some people who ported from Qt 3 to Qt 4, who used seek() and pos() together to navigate text files.

Thiago Macieira and I found a beautiful solution to this problem, and I just submitted QTextStream::pos() into what will become Qt 4.2. Together with QTextStream::seek(), which does what QTextStream::device()->seek() did in Qt 3, you can do exactly what you could in Qt 3, but it's just faster than before.

In addition, Qt 3's textstream class has been ported into Qt3Support, so you'll see a Q3TextStream class which will make porting even easier.

Subscribe to Our Blog

Stay up to date with the latest marketing, sales and service tips and news.

We are updating our comment system and you could face some issues. Please write to us at feedback@qt.io to report issues/bugs.