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:
- append: This appends one QString class onto another.
- arg: This is used to build up formatted strings (instead of sprintf).
- at and operator[]: These you can use to access a single character in a QString.
- operator==, operator!=, operator<, operator>, operator<=, and operator>=: These compare two QStrings.
- clear: This empties a QString and sets it to the null string.
- contains: This searches one string for another string or a regular expression.
- count: This counts the occurrences of a substring or character in a QString.
- startsWith and endsWith: These return true if a QString starts or ends with a specific string, respectively.
- indexOf: This returns the index of the first occurrence of a substring in a string, or -1 if the substring doesn't exist in the string.
- insert: This inserts another QString at a specific position in a QString.
- lastIndexOf: This returns the last index of a substring in a QString.
- length: This returns the length of a string in characters.
- remove: This removes all occurrences or a number of characters of a string from QString.
- setNum: This formats a number and replaces the value of the QString with the given number.
- split: This returns a list of QString objects (we'll discuss Qt lists in a moment) created by splitting the string at a specific separator character.
- toDouble, toFloat, toInt, and toLong: These return numeric representations of the string if a conversion is possible.
- toLower and toUpper: These return a copy of the string converted to
lower- or uppercase. - truncate: This truncates the string at a given position.
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:
- append: This appends an item to the list.
- at: This accesses an individual element of the list for read-only purposes.
- clear: This empties the list.
- contains: This searches the list for a specific element.
- count: This counts the number of times an element occurs in the list.
- empty: This returns true if the list is empty.
- startsWith and endsWith: These return true if the list begins or ends with the specified element.
- first and last: These return the first and last element of the list, respectively.
- indexOf: This returns the index of the first occurrence of an item if it's in the list, or -1 if the item is not found.
- lastIndexOf: This returns the last index of an item if it's in the list.
- length: This returns the length of the list.
- prepend: This prepends an item to the list.
- push_back and push_front: These push an element to the end or the beginning of the list, respectively.
- removeAt, removeFirst, and removeLast: These remove the ith, first, or last element of the list.
- replace: This replaces an element of the list.
- swap: This swaps two elements of the list at different indices.
- toStdList: This returns std::list<T> of the QList.
- toVector: This returns QVector<T> of the list.
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.