Representing data using Qt's core classes

Probably the most common Qt core class you'll run into is Qt's QString container class for character strings. It has similar capabilities to the C++ STL std::wstring class. As with wstring, it's multibyte. You can construct one from a traditional C-style char * string or another QString.

QString has lots of helper methods, some of which are as follows:

Qt also has a number of template collection classes. The most general of these is QList<T>, which is optimized for fast index-based access as well as fast insertions and deletions. There's also QLinkedList<T>, which uses a linked list structure to store its values, and QVector<T>, which stores its elements in a serial vector array, so the use of template classes is the fastest for indexed access, but is slow to resize. Qt also provides QStringList, which is the same as QList<QString> for all intents and purposes.

As you might imagine, these types provide  operator[] , so you can access and assign any element of the list. Other QList<T> methods you'll find include:

A common thing you'll want to do with a list is iterate over its elements. QList provides iterators similar to how the C++ STL does, so you can iterate over the elements of a list like this:

QList<QString> list; 
list.append("January"); 
list.append("February"); 
 ... 
list.append("December"); 
 
QList<QString>::const_iterator i; 
for (i = list.constBegin(); i != list.constEnd(); ++i) 
    cout << *i << endl; 

QList<T>::const_iterator and QList<T>::iterator provide read-only and mutable iterators over the list; you can obtain one by calling constBegin or begin, respectively, and compare it against constEnd or end to see when you're at the end of a list.

Now that we know how the core classes work, let us see how they work with key-value pairs.