problem in porting QString::at(index)

Thiago Macieira thiago at
Tue Oct 11 21:15:36 BST 2005

David Faure wrote:
>On Tuesday 11 October 2005 15:48, Andreas Gungl wrote:
>> I've found a problem in a changed semantic of QString::at(0) which
>> seemed to return '\0' in Qt3 for an empty string. Now we run into an
>> assertion for 0 <= i < size() although the Qt4 docu says about
>> QString::size(), that at position size() of a QString there is ever
>> placed a '\0'.
>From what you say there is clearly a mismatch between the Qt4 behavior
> and the Qt4 documentation, so you should send this mail to
> qt-bugs at

I don't agree. It just looks like a misinterpretation of the docs:

	const QChar QString::at ( int i ) const

	Returns the character at index position i in the string.

	i must be a valid index position in the string (i.e., 0 <= i < size()).

	See also operator[]().

	int QString::size () const

	Returns the number of characters in this string.

	The last character in the string is at position size() - 1. In addition, 
	QString ensures that the character at position size() is always '\0', so 
	that you can use the return value of data() and constData() as arguments 
	to functions that expect '\0'-terminated strings.

So, it means you can use data() and constData() and read U+0000-terminated 
strings, but you're not allowed to use at() to access it.

For this case, I recommend to use constData() directly. The difference 
between doing that and using QString::at is the bounds check.

  Thiago Macieira  -  thiago (AT) - thiago (AT)
    PGP/GPG: 0x6EF45358; fingerprint:
    E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358

1. On frumscafte, hwonne time_t wæs náht, se scieppend þone circolwyrde 
wundorcræftlíge cennede and seo eorðe wæs idel and hit wæs gód.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <>

More information about the kde-core-devel mailing list