QDict Class ReferenceThe QDict class is a template class that provides a dictionary based on QString keys. More... #include <qdict.h> Inherits QPtrCollection. Public Members
Important Inherited Members
Protected Members
Detailed DescriptionThe QDict class is a template class that provides a dictionary based on QString keys.
QMap is an STL-compatible alternative to this class. QDict is implemented as a template class. Define a template instance QDict<X> to create a dictionary that operates on pointers to X (X *). A dictionary is a collection of key-value pairs. The key is a QString used for insertion, removal and lookup. The value is a pointer. Dictionaries provide very fast insertion and lookup. If you want to use non-Unicode, plain 8-bit char* keys, use the QAsciiDict template. A QDict has the same performance as a QAsciiDict. If you want to have a dictionary that maps QStrings to QStrings use QMap. The size() of the dictionary is very important. In order to get good performance, you should use a suitably large prime number. Suitable means equal to or larger than the maximum expected number of dictionary items. Size is set in the constructor but may be changed with resize(). Items are inserted with insert(); 0 pointers cannot be inserted. Items are removed with remove(). All the items in a dictionary can be removed with clear(). The number of items in the dictionary is returned by count(). If the dictionary contains no items isEmpty() returns TRUE. You can change an item's value with replace(). Items are looked up with operator[](), or with find() which return a pointer to the value or 0 if the given key does not exist. You can take an item out of the dictionary with take(). Calling setAutoDelete(TRUE) for a dictionary tells it to delete items that are removed. The default behaviour is not to delete items when they are removed. When an item is inserted, the key is converted (hashed) to an integer index into an internal hash array. This makes lookup very fast. Items with equal keys are allowed. When inserting two items with the same key, only the last inserted item will be accessible (last in, first out) until it is removed. The QDictIterator class can traverse the dictionary, but only in an arbitrary order. Multiple iterators may independently traverse the same dictionary. When inserting an item into a dictionary, only the pointer is copied, not the item itself, i.e. a shallow copy is made. It is possible to make the dictionary copy all of the item's data (a deep copy) when an item is inserted. insert() calls the virtual function QPtrCollection::newItem() for the item to be inserted. Inherit a dictionary and reimplement newItem() if you want deep copies. When removing a dictionary item, the virtual function QPtrCollection::deleteItem() is called. QDict's default implementation is to delete the item if auto-deletion is enabled. Example #1: QDict<QLineEdit> fields; // QString keys, QLineEdit* values fields.insert( "forename", new QLineEdit( this ) ); fields.insert( "surname", new QLineEdit( this ) ); fields["forename"]->setText( "Homer" ); fields["surname"]->setText( "Simpson" ); QDictIterator<QLineEdit> it( fields ); // See QDictIterator for( ; it.current(); ++it ) cout << it.currentKey() << ": " << it.current()->text() << endl; cout << endl; if ( fields["forename"] && fields["surname"] ) cout << fields["forename"]->text() << " " << fields["surname"]->text() << endl; // Prints "Homer Simpson" fields.remove( "forename" ); // Does not delete the line edit if ( ! fields["forename"] ) cout << "forename is not in the dictionary" << endl;In this example we use a dictionary to keep track of the line edits we're using. We insert each line edit into the dictionary with a unique name and then access the line edits via the dictionary. Example #2: QStringList styleList = QStyleFactory::styles(); styleList.sort(); QDict<int> letterDict( 17, FALSE ); for ( QStringList::Iterator it = styleList.begin(); it != styleList.end(); ++it ) { QString styleName = *it; QString styleAccel = styleName; if ( letterDict[styleAccel.left(1)] ) { for ( uint i = 0; i < styleAccel.length(); i++ ) { if ( ! letterDict[styleAccel.mid( i, 1 )] ) { styleAccel = styleAccel.insert( i, '&' ); letterDict.insert(styleAccel.mid( i, 1 ), (const int *)1); break; } } } else { styleAccel = "&" + styleAccel; letterDict.insert(styleAccel.left(1), (const int *)1); } (void) new QAction( styleName, QIconSet(), styleAccel, parent ); }In the example we are using the dictionary to provide fast random access to the keys, and we don't care what the values are. The example is used to generate a menu of QStyles, each with a unique accelerator key (or no accelerator if there are no unused letters left). We first obtain the list of available styles, then sort them so that the menu items will be ordered alphabetically. Next we create a dictionary of int pointers. The keys in the dictionary are each one character long, representing letters that have been used for accelerators. We iterate through our list of style names. If the first letter of the style name is in the dictionary, i.e. has been used, we iterate over all the characters in the style name to see if we can find a letter that hasn't been used. If we find an unused letter we put the accelerator ampersand (&) in front of it and add that letter to the dictionary. If we can't find an unused letter the style will simply have no accelerator. If the first letter of the style name is not in the dictionary we use it for the accelerator and add it to the dictionary. Finally we create a QAction for each style.
See also QDictIterator, QAsciiDict, QIntDict, QPtrDict, Collection Classes, and Non-GUI Classes. Member Function Documentation
|
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