QDict(3qt)
π QDict π
aka QDict(3qt) β the Qt dictionary page, bestie edition π«Ά
β¨ NAME β¨
QDict</p> β a template class that hands you a whole entire dictionary, and it's built on QString keys. periodt.
β¨ SYNOPSIS β¨
#include <qdict.h>
It inherits QPtrCollection. π
πΈ Public Members πΈ
QDict ( int size = 17, bool caseSensitive = TRUE )
QDict ( const QDict<type> & dict )
~QDict ()
QDict<type> & operator= ( const QDict<type> & dict )
virtual uint count () const
uint size () const
bool isEmpty () const
void insert ( const QString & key, const type * item )
void replace ( const QString & key, const type * item )
bool remove ( const QString & key )
type * take ( const QString & key )
type * find ( const QString & key ) const
type * operator[] ( const QString & key ) const
virtual void clear ()
void resize ( uint newsize )
void statistics () const
π Important Inherited Members π
bool autoDelete () const
void setAutoDelete ( bool enable )
π¦ Protected Members π¦
virtual QDataStream & read ( QDataStream & s, QPtrCollection::Item &
item )
virtual QDataStream & write ( QDataStream & s, QPtrCollection::Item )
const
β¨ DESCRIPTION β¨
okay so. The QDict class is a template class that provides a dictionary based on QString keys. ποΈ
QMap is an STL-compatible alternative to this class, if that's more your vibe.
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 instead. A QDict has the same performance as a QAsciiDict. And if you want 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 returns 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 β¨
π QDict::QDict ( int size = 17, bool caseSensitive = TRUE )
Constructs a dictionary optimized for less than size entries.
We recommend setting size to a suitably large prime number (e.g. a prime that's slightly larger than the expected number of entries). This makes the hash distribution better which will lead to faster lookup. β‘
If caseSensitive is TRUE (the default), keys which differ only by case are considered different. π
π QDict::QDict ( const QDict<type> & dict )
Constructs a copy of dict.
Each item in dict is inserted into this dictionary. Only the pointers are copied (shallow copy).
π QDict::~QDict ()
Removes all items from the dictionary and destroys it. If setAutoDelete() is TRUE, each value is deleted. All iterators that access this dictionary will be reset. π
See also setAutoDelete().
π bool QPtrCollection::autoDelete () const
Returns the setting of the auto-delete option. The default is FALSE.
See also setAutoDelete().
π void QDict::clear () [virtual]
Removes all items from the dictionary.
The removed items are deleted if auto-deletion is enabled.
All dictionary iterators that operate on the dictionary are reset. π§Ή
See also remove(), take(), and setAutoDelete().
Reimplemented from QPtrCollection.
π uint QDict::count () const [virtual]
Returns the number of items in the dictionary. π’
See also isEmpty().
Reimplemented from QPtrCollection.
π type * QDict::find ( const QString & key ) const
Returns the item with key key, or 0 if the key does not exist in the dictionary.
If there are two or more items with equal keys, then the most recently inserted item will be found. π
Equivalent to the [] operator.
See also operator[]().
π void QDict::insert ( const QString & key, const type * item )
Inserts the key key with value item into the dictionary.
Multiple items can have the same key, in which case only the last item will be accessible using operator[](). π
itemmay not be0.
See also replace().
Example: themes/themes.cpp.
π bool QDict::isEmpty () const
Returns TRUE if the dictionary is empty, i.e. count() == 0; otherwise returns FALSE. π«
See also count().
π QDict<type> & QDict::operator= ( const QDict<type> & dict )
Assigns dict to this dictionary and returns a reference to this dictionary.
This dictionary is first cleared, then each item in dict is inserted into this dictionary. Only the pointers are copied (shallow copy), unless newItem() has been reimplemented. π
π type * QDict::operator[] ( const QString & key ) const
Returns the item with key key, or 0 if the key does not exist in the dictionary.
If there are two or more items with equal keys, then the most recently inserted item will be found. π
Equivalent to the find() function.
See also find().
π QDataStream & QDict::read ( QDataStream & s, QPtrCollection::Item & item ) [virtual protected]
Reads a dictionary item from the stream s and returns a reference to the stream.
The default implementation sets item to 0. π
See also write().
π bool QDict::remove ( const QString & key )
Removes the item with key from the dictionary. Returns TRUE if successful, i.e. if the item is in the dictionary; otherwise returns FALSE.
If there are two or more items with equal keys, then the last item that was inserted will be removed. ποΈ
The removed item is deleted if auto-deletion is enabled.
All dictionary iterators that refer to the removed item will be set to point to the next item in the dictionary's traversal order. π§
See also take(), clear(), and setAutoDelete().
void QDict::replace ( const QString & key, const type * item ) Replaces the value of the key, key with item.
If the item does not already exist, it will be inserted.
item may not be 0.
Equivalent to:
QDict<char> dict; ... if ( dict.find( key ) ) dict.remove( key ); dict.insert( key, item );
If there are two or more items with equal keys, then the last item that was inserted will be replaced.
See also insert().
π MEMBER FUNCTION DOCUMENTATION π (part 2, we're so back)
void QDict::replace ( const QString & key, const type * item )
Okay bestie, this one is lowkey just a glow-up for an existing entry β¨ β it swaps out the value sitting under key and drops item in its place. Replaced, refreshed, rebranded. π
And if that item was never in the dict to begin with? No worries, it just gets inserted like it was always meant to be there. Main character energy fr.
β οΈ Heads up: item may not be 0. Don't do it. Not the vibe.
It's literally equivalent to doing all this by hand:
QDict<char> dict;
...
if ( dict.find( key ) )
dict.remove( key );
dict.insert( key, item );
And the plot twist π«’ β if there are two or more items with equal keys, the last item that was inserted is the one that gets replaced. Last in, first to get swapped. Periodt.
See also insert().
void QDict::resize ( uint newsize )
This resizes the internal hash table to newsize π. The contents of the dictionary are preserved β nothing gets yeeted, we're just rearranging the furniture ποΈβ¨.
But here's the tea: all iterators on the dictionary become invalid. Every single one. They're done. Expired. Not invited to the function anymore π. So if you were holding onto an iterator, that's on you, bestie.
void QPtrCollection::setAutoDelete ( bool enable )
Sets the collection to auto-delete its contents if enable is TRUE, and to never delete them if enable is FALSE. It's giving "set it and forget it" but also "set it and regret it" depending on how you play it π.
If auto-deleting is turned on, all the items in a collection get deleted when the collection itself is deleted. That's genuinely convenient if the collection holds the only pointer to the items β chef's kiss, no leaks, slay π.
The default setting is FALSE, for safety. Because safety is cute. If you turn it on, be careful about copying the collection β you might find yourself with two collections deleting the same items. That's not a vibe, that's a double-free waiting to happen π.
π Note that the auto-delete setting may also affect other functions in subclasses. For example, a subclass that has a remove() function will remove the item from its data structure, and if auto-delete is enabled, will also delete the item. Sneaky, but documented, so we're fine.
See also autoDelete().
Examples:
(the original page lists no example bodies here, so nothing to show, bestie β that's just how it came out π)
uint QDict::size () const
Returns the size of the internal hash array β the one you specified in the constructor π¦. It's the capacity vibe, not the population vibe.
See also count().
void QDict::statistics () const
A debugging-only function that prints out the dictionary distribution using qDebug(). It's basically your dict spilling its own stats to the console for the group chat π. Dev-only, don't ship it as a feature, iykyk.
type * QDict::take ( const QString & key )
Takes the item with key out of the dictionary without deleting it β even if auto-deletion is enabled π€. Like, you're rescuing it from the void before the cleanup crew arrives.
If there are two or more items with equal keys, then the last item that was inserted will be taken. Same rule as always, bestie.
Returns a pointer to the item taken out, or 0 if the key does not exist in the dictionary. (That 0 is the "nobody home" answer π.)
All dictionary iterators that refer to the taken item will be set to point to the next item in the dictionary traversal order. So they don't dangle β they just get redirected, iconic behaviour honestly π.
See also remove(), clear(), and setAutoDelete().
QDataStream & QDict::write ( QDataStream & s, QPtrCollection::Item ) const
[virtual protected]
Writes a dictionary item to the stream s and returns a reference to the stream π. Serialization moment.
See also read().
β¨ SEE ALSO β¨
π COPYRIGHT π
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.
πΈ AUTHOR πΈ
Generated automatically from the source code.
π BUGS π
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you.
The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.
If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qdict.3qt) and the Qt version (3.3.8).
And that's a wrap on QDict, bestie π«Άβ¨ this page was generated from Trolltech's docs for the Qt 3.3.8 release, dated 2 February 2007 β yes, it's vintage, and yes, it still slays. Now go forth and hash responsibly π
π¦ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms