Collection Classes A collection class is a container which holds a number of items in a
data structure and provides various operations to manipulate the
contents of the collection, such as insert item, remove item, find
item, etc.
Qt has several value-based and several pointer-based collection
classes. The pointer-based collection classes work with pointers to
items, while the value-based classes store copies of their items. The
value-based collections are very similar to STL container classes, and
can be used with STL algorithms and containers. See the Qt Template Library documentation for
details.
The value-based collections are:
The pointer-based collections are:
QMemArray is exceptional; it is neither pointer nor value based,
but memory based. For maximum efficiency with the simple data types
usually used in arrays, it uses bitwise operations to copy and compare
array elements.
Some of these classes have corresponding iterators. An iterator
is a class for traversing the items in a collection:
The value-based collections plus algorithms operating on them are
grouped together in the Qt Template
Library; see also the Qt Template
Library Classes.
The rest of this page dicusses the pointer-based containers.
There are four internal base classes for the pointer-based
containers (QGCache, QGDict, QGList and QGVector) that operate on
void pointers. A thin template layer implements the actual
collections by casting item pointers to and from void pointers.
This strategy allows Qt's templates to be very economical on space
(instantiating one of these templates adds only inlinable calls to
the base classes), without hurting performance.
This example shows how to store Employee items in a list and prints
them out in reverse order:
Program output:
All pointer-based collections inherit the QPtrCollection base class.
This class only knows about the number of items in the collection and
the deletion strategy.
By default, items in a collection are not deleted when they are
removed from the collection. The QPtrCollection::setAutoDelete()
function specifies the deletion strategy. In the list example, we
enable auto-deletion to make the list delete the items when they are
removed from the list.
When inserting an item into a collection, only the pointer is copied,
not the item itself. This is called a shallow copy. It is possible to
make the collection copy all of the item's data (known as a deep copy)
when an item is inserted. All collection functions that insert an
item call the virtual function QPtrCollection::newItem() for the item
to be inserted. Inherit a collection and reimplement it if you want
to have deep copies in your collection.
When removing an item from a list, the virtual function
QPtrCollection::deleteItem() is called. The default implementation
in all collection classes deletes the item if auto-deletion is
enabled.
A pointer-based collection class, such as QPtrList<type>, defines a
collection of pointers to type objects. The pointer (*) is
implicit.
We discuss QPtrList here, but the same techniques apply to all
pointer-based collection classes and all collection class iterators.
Template instantiation:
The item's class or type, Employee in our example, must be defined prior
to the list definition.
Although QPtrList has member functions to traverse the list, it can
often be better to make use of an iterator. QPtrListIterator is very
safe and can traverse lists that are being modified at the same time.
Multiple iterators can work independently on the same collection.
A QPtrList has an internal list of all the iterators that are
currently operating on it. When a list entry is removed, the list
updates all iterators accordingly.
The QDict and QCache collections have no traversal functions. To
traverse these collections, you must use QDictIterator or QCacheIterator.
Qt has the following predefined collection classes:
In almost all cases you would choose QStringList, a value
list of implicitly shared QString Unicode strings. QPtrStrList and
QPtrStrIList store only char pointers, not the strings themselves.
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le blog Digia au hasardCréer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut CuvelierLe blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.
CommunautéRessources
Liens utilesContact
Qt dans le magazine |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 3.2 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com