Any time you have a container of things, sooner or later you are probably going to loop through the container and do something with each thing. An iterator is an object that provides indirect access to each element in a container. It is specifically designed to be used in a loop.

Qt 4 supports the following styles of iteration:

  1. Qt 4 style foreach loops, similar to Perl and Python
  2. Java 1.2 style Iterator
  3. Standard Library style ContainerType::iterator
  4. Hand-made while or for loops that use getters of the container

The next section demonstrates the various styles of iteration available in C++ with Qt 4.

4.2.1. QStringList and Iteration

For text processing, it is very useful to work with lists of strings. QStringList is derived from QList so it inherits all of QList's behavior (see Chapter 6). In addition, QStringList has some string-specific convenience functions such as indexOf(), join(), and replaceInStrings().

Converting between lists and individual strings is quite easy with perl-like split() and join() functions. Example 4.1 demonstrates lists, iterations, split(), and join().

Example 4.1. src/collections/lists/lists-examples.cpp


/* Some simple examples using QStringList, split and join */
int main() {

 QString winter = "December, January, February";
 QString spring = "March, April, May";
 QString summer = "June, July, August";
 QString fall = "September, October, November";

 QStringList list;
 list << winter; <-- 1
 list += spring; <-- 2
 list.append(summer); <-- 3
 list << fall;

 qDebug() << "The Spring months are: " << list[1] ;

 QString allmonths = list.join(", ");
 /* from list to string - join with a ", " delimiter */
 qDebug() << allmonths;

 QStringList list2 = allmonths.split(", ");
 /* split is the opposite of join. Each month will have its
 own element. */

 assert(list2.size() == 12); <-- 4

 foreach (QString str, list) { <-- 5
 qDebug() << QString(" [%1] ").arg(str);

 for (QStringList::iterator it = list.begin();
 it != list.end(); ++it) { <-- 6
 QString current = *it; <-- 7
 qDebug() << "[[" << current << "]]";

 QListIterator itr (list2); <-- 8
 while (itr.hasNext()) { <-- 9
 QString current =;
 qDebug() << "{" << current << "}";

 return 0;

(1)append operator 1

(2)append operator 2

(3)append member function

(4)Assertions abort the program if the condition is not satisfied.

(5)Qt 4 foreach loop, similar to Perl/Python and Java 1.5 style for loops.

(6)C++ STL-style iteration

(7)pointer-style dereference

(8)Java 1.2 style iterator

(9)Java iterators point in between elements.

src/collections> ./collections
The Spring months are: March, April, May
December, January, February, March, April, May, June, July, August,
September, October, November


Part I: Introduction to C++ and Qt 4

C++ Introduction


Introduction to Qt



Inheritance and Polymorphism

Part II: Higher-Level Programming


Introduction to Design Patterns


Generics and Containers

Qt GUI Widgets


Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments


MP3 Jukebox Assignments

An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: