Announcements | Last Post | |
---|---|---|
SoC Curricula | 09/30/2017 01:08PM | |
Demarcation or scoping of examinations and assessment | 02/13/2017 07:59AM | |
School of Computing Short Learning Programmes | 11/24/2014 08:37AM | |
Unisa contact information | 07/28/2011 01:28PM |
QList does not name a type June 25, 2008 12:42PM |
Registered: 16 years ago Posts: 239 Rating: 0 |
Re: QList does not name a type June 25, 2008 04:49PM |
Registered: 17 years ago Posts: 23 Rating: 0 |
Re: QList does not name a type June 25, 2008 05:07PM |
Registered: 16 years ago Posts: 239 Rating: 0 |
Re: QList does not name a type June 25, 2008 11:48PM |
Registered: 18 years ago Posts: 1,424 Rating: 0 |
Quote
Detailed Description
The QList class is a template class that provides lists.
QList<T> is one of Qt's generic container classes. It stores a list of values and provides fast index-based access as well as fast insertions and removals.
QList<T>, QLinkedList<T>, and QVector<T> provide similar functionality. Here's an overview:
For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API, and it is usually faster than QVector because of the way it stores its items in memory. It also expands to less code in your executable.
If you need a real linked list, with guarantees of constant time insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList.
If you want the items to occupy adjacent memory positions, use QVector.
Internally, QList<T> is represented as an array of pointers to items. (Exceptionally, if T is a pointer type, a basic type of the size of a pointer, or one of Qt's shared classes, QList<T> stores the item directly in the pointer.) For lists under a thousand items, this representation allows for very fast insertions in the middle, in addition to instantaneous index-based access. Furthermore, operations like prepend() and append() are very fast, because QList preallocates memory on both sides of its internal array.
Here's an example of a QList that stores integers and a QList that stores QDate values:
QList<int> integerList;
QList<QDate> dateList;
Qt includes a QStringList class that inherits QList<QString> and adds a few convenience functions, such as QStringList::join() and QStringList::find(). (QString::split() creates QStringLists from strings.)
QList stores a list of items. The default constructor creates an empty list. To insert items into the list, you can use operator<<():
QList<QString> list;
list << "one" << "two" << "three";
// list: ["one", "two", "three"]
QList provides these basic functions to add, move, and remove items: insert(), replace(), removeAt(), move(), and swap(). In addition, it provides the following convenience functions: append(), prepend(), removeFirst(), and removeLast().
QList uses 0-based indexes, just like C++ arrays. To access the item at a particular index position, you can use operator[](). On non-const lists, operator[]() returns a reference to the item and can be used on the left side of an assignment:
if (list[0] == "Bob"
list[0] = "Robert";
Because QList is implemented as an array of pointers, this operation is very fast (constant time). For read-only access, an alternative syntax is to use at():
for (int i = 0; i < list.size(); ++i) {
if (list.at(i) == "Jane"
cout << "Found Jane at position " << i << endl;
}
at() can be faster than operator[](), because it never causes a deep copy to occur.
A common requirement is to remove an item from a list and do something with it. For this, QList provides takeAt(), takeFirst(), and takeLast(). Here's a loop that removes the items from a list one at a time and calls delete on them:
QList<QWidget *> list;
...
while (!list.isEmpty())
delete list.takeFirst();
Inserting and removing items at either ends of the list is very fast (constant time in most cases), because QList preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.
If you want to find all occurrences of a particular value in a list, use indexOf() or lastIndexOf(). The former searches forward starting from a given index position, the latter searches backward. Both return the index of a matching item if they find it; otherwise, they return -1. For example:
int i = list.indexOf("Jane"
if (i != -1)
cout << "First occurrence of Jane is at position " << i << endl;
If you simply want to check whether a list contains a particular value, use contains(). If you want to find out how many times a particular value occurs in the list, use count(). If you want to replace all occurrences of a particular value with another, use replace().
QList's value type must be an assignable data type. This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *. A few functions have additional requirements; for example, indexOf() and lastIndexOf() expect the value type to support operator==(). These requirements are documented on a per-function basis.
Like the other container classes, QList provides Java-style iterators (QListIterator and QMutableListIterator) and STL-style iterators (QList::const_iterator and QList::iterator). In practice, these are rarely used, because you can use indexes into the QList. QList is implemented in such a way that direct index-based access is just as fast as using iterators.
QList does not support inserting, prepending, appending or replacing with references to its own values. Doing so will cause your application to abort with an error message.
See also QListIterator, QMutableListIterator, QLinkedList, and QVector.
Re: QList does not name a type June 26, 2008 08:40AM |
Registered: 17 years ago Posts: 191 Rating: 0 |
Re: QList does not name a type June 26, 2008 10:30AM |
Registered: 16 years ago Posts: 239 Rating: 0 |
Re: QList does not name a type June 26, 2008 01:01PM |
Registered: 17 years ago Posts: 23 Rating: 0 |
Re: QList does not name a type June 26, 2008 01:12PM |
Registered: 17 years ago Posts: 191 Rating: 0 |
Re: QList does not name a type June 26, 2008 02:14PM |
Registered: 16 years ago Posts: 239 Rating: 0 |