Buffered QTextStream + pos() == ?

Published Saturday June 17th, 2006
Comments Off on Buffered QTextStream + pos() == ?
Posted in Uncategorized

[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.

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in Uncategorized

Get started today with Qt Download now