Qt Containers - Lists
I selected this topic due to it's scope and unawareness in new programmers and there is no exception for new Qt programmers as well. Containers, as the name defines, are those structures that are specially designed to hold the data. After holding it, it allows certain operations, not to manipulate it, but to arrange it in specific order and provides accessibility to it.
Qt has several built-in containers. The good point of Qt Container classes is that it allows conversion to STL Container classes and also overloaded operators for easy usage.
Let's discuss few of Container classes in Qt:
1. QList<T>
QList<int> lst;
lst.append(5);
lst.append(11);
lst.append(0);
lst.append(1);
for(int i = 0; i < lst.count(); i++){
cout << lst[i] << ", ";
}
Qt has several built-in containers. The good point of Qt Container classes is that it allows conversion to STL Container classes and also overloaded operators for easy usage.
Let's discuss few of Container classes in Qt:
1. QList<T>
You can consider it like a dynamic array. It allows access to data from both sides and also by the index, like an array. It allows converting to QVector<T> and STL vectors as well. When its object is initialized, it pre-allocate the space of its data. It has good performance in prepend() and append() operations.
lst.append(5);
lst.append(11);
lst.append(0);
lst.append(1);
for(int i = 0; i < lst.count(); i++){
cout << lst[i] << ", ";
}
The important thing to note at here is that the sequence will remain same in which the items are added/appended in to the list.
2. QStringList
QStringList is derived from QList<QString>. It has several convenience functions for sorting, duplicates, joining, filter, regular expressions, sorting etc. Similarly, it has overloaded operators for more convenient use of this class. like insertion operator <<.
QStringList countries;
//adding strings using '<<' operator
fonts << "Pakistan" << "Iran" << "China" << "Russia";
//to get comma seperated countries
//Pakistan,Iran,China,Russia
QString allCountries = countries.join(",");
//and to get back countries list form allCountries
countries.clear(); //(optional) to clear the list
countries = allCountries.split(",");
QStringList countries;
//adding strings using '<<' operator
fonts << "Pakistan" << "Iran" << "China" << "Russia";
//to get comma seperated countries
//Pakistan,Iran,China,Russia
QString allCountries = countries.join(",");
//and to get back countries list form allCountries
countries.clear(); //(optional) to clear the list
countries = allCountries.split(",");
Using QStringList instead of QList<String> provides lot of flexibility to the developer. It inherits the performance of QList<T>, hence it provides fast index based access and fast insertion and removal.
3. QLinkedList<T>
As the name suggests, its elements can be spread at different location in the memory, hence it is called Linked. Qt has optimized it for sequential access with iterators and quick insert anywhere in the list. However, as its elements are scattered, the sorting and searching is slow.
Conclusion on Lists:
- 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.
Next I will continue with Vector, Stack, Map, Sets and other containers available in Qt.
Thank you for reading this. Please let me know how this is helpful to you but adding you comments.
nice. very informative :)
ReplyDeleteplz provide more information on QVector
ReplyDelete