There is also a value() overload that uses its second argument as a default value if there is no item with the specified key:

int timeout = hash.value("TIMEOUT",30);

In general, we recommend that you use contains() and value() rather than operator[]() for looking up a key in a hash. The reason is that operator[]() silently inserts an item into the hash if no item exists with the same key (unless the hash is const). For example, the following code snippet will create 1000 items in memory:

To avoid this problem, replace hash[i] with hash.value(i) in the code above.

Internally, QHash uses a hash table to perform lookups. This hash table automatically grows and shrinks to provide fast lookups without wasting too much memory. You can still control the size of the hash table by calling reserve() if you already know approximately how many items the QHash will contain, but this isn't necessary to obtain good performance. You can also call capacity() to retrieve the hash table's size.

However, you can store multiple values per key by using insertMulti() instead of insert() (or using the convenience subclass QMultiHash). If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>:

The items that share the same key are available from most recently to least recently inserted. A more efficient approach is to call find() to get the iterator for the first item with a key and iterate from there:

Items can be removed from the hash in several ways. One way is to call remove(); this will remove any item with the given key. Another way is to use QMutableHashIterator::remove(). In addition, you can clear the entire hash using clear().

The qHash() hashing function

A QHash's key type has additional requirements other than being an assignable data type: it must provide operator==(), and there must also be a qHash() function in the type's namespace that returns a hash value for an argument of the key's type.

The qHash() function computes a numeric value based on a key. It can use any algorithm imaginable, as long as it always returns the same value if given the same argument. In other words, if e1 == e2, then qHash(e1) == qHash(e2) must hold as well. However, to obtain good performance, the qHash() function should attempt to return different hash values for different keys to the largest extent possible.

For a key type K, the qHash function must have one of these signatures:

The two-arguments overloads take an unsigned integer that should be used to seed the calculation of the hash function. This seed is provided by QHash in order to prevent a family of algorithmic complexity attacks. If both a one-argument and a two-arguments overload are defined for a key type, the latter is used by QHash (note that you can simply define a two-arguments version, and use a default value for the seed parameter).

Here's a partial list of the C++ and Qt types that can serve as keys in a QHash: any integer type (char, unsigned long, etc.), any pointer type, QChar, QString, and QByteArray. For all of these, the <QHash> header defines a qHash() function that computes an adequate hash value. Many other Qt classes also declare a qHash overload for their type; please refer to the documentation of each class.

If you want to use other types as the key, make sure that you provide operator==() and a qHash() implementation.

In the example above, we've relied on Qt's global qHash(const QString &, uint) to give us a hash value for the employee's name, and XOR'ed this with the day they were born to help produce unique hashes for people with the same name.

Note that the implementation of the qHash() overloads offered by Qt may change at any time. You must not rely on the fact that qHash() will give the same results (for the same inputs) across different Qt versions.

Algorithmic complexity attacks

All hash tables are vulnerable to a particular class of denial of service attacks, in which the attacker carefully pre-computes a set of different keys that are going to be hashed in the same bucket of a hash table (or even have the very same hash value). The attack aims at getting the worst-case algorithmic behavior (O(n) instead of amortized O(1), see Algorithmic Complexity for the details) when the data is fed into the table.

In order to avoid this worst-case behavior, the calculation of the hash value done by qHash() can be salted by a random seed, that nullifies the attack's extent. This seed is automatically generated by QHash once per process, and then passed by QHash as the second argument of the two-arguments overload of the qHash() function.

This randomization of QHash is enabled by default. Even though programs should never depend on a particular QHash ordering, there may be situations where you temporarily need deterministic behavior, for example for debugging or regression testing. To disable the randomization, define the environment variable QT_HASH_SEED to have the value 0. Alternatively, you can call the qSetGlobalQHashSeed() function with the value 0.

int QHash::capacity() const

The sole purpose of this function is to provide a means of fine tuning QHash's memory usage. In general, you will rarely ever need to call this function. If you want to know how many items are in the hash, call size().

Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator to the next item in the hash.

Unlike remove() and take(), this function never causes QHash to rehash its internal data structure. This means that it can safely be called while iterating, and won't affect the order of items in the hash. For example:

If the hash contains no item with the key, the function returns end().

If the hash contains multiple items with the key, this function returns an iterator that points to the most recently inserted value. The other values are accessible by incrementing the iterator. For example, here's some code that iterates over all the items with the same key:

Returns a list containing all the keys in the hash, in an arbitrary order. Keys that occur multiple times in the hash (because items were inserted with insertMulti(), or unite() was used) also occur multiple times in the list.

To obtain a list of unique keys, where each key from the map only occurs once, use uniqueKeys().

Returns a list containing all the keys associated with value value, in an arbitrary order.

This function can be slow (linear time), because QHash's internal data structure is optimized for fast lookup by key, not by value.

int QHash::remove(const Key &key)

Removes all the items that have the key from the hash. Returns the number of items removed which is usually 1 but will be 0 if the key isn't in the hash, or greater than 1 if insertMulti() has been used with the key.

Ideally, size should be slightly more than the maximum number of items expected in the hash. size doesn't have to be prime, because QHash will use a prime number internally anyway. If size is an underestimate, the worst that will happen is that the QHash will be a bit slower.

In general, you will rarely ever need to call this function. QHash's internal hash table automatically shrinks or grows to provide good performance without wasting too much memory.

const T QHash::value(const Key &key) const

Returns the value associated with the key.

If the hash contains no item with the key, the function returns a default-constructed value. If there are multiple items for the key in the hash, the value of the most recently inserted one is returned.

const T QHash::value(const Key &key, const T &defaultValue) const

Returns a list containing all the values in the hash, in an arbitrary order. If a key is associated with multiple values, all of its values will be in the list, and not just the most recently inserted one.

T &QHash::operator[](const Key &key)

Returns the value associated with the key as a modifiable reference.

If the hash contains no item with the key, the function inserts a default-constructed value into the hash with the key, and returns a reference to it. If the hash contains multiple items with the key, this function returns a reference to the most recently inserted value.

This takes advantage of the fact that std::vector lays out its data contiguously. If that is not the case, or the contained type has padding, you should use qHashRange() instead.

It bears repeating that the implementation of qHashBits() - like the qHash() overloads offered by Qt - may change at any time. You must not rely on the fact that qHashBits() will give the same results (for the same inputs) across different Qt versions.

It bears repeating that the implementation of qHashRange() - like the qHash() overloads offered by Qt - may change at any time. You must not rely on the fact that qHashRange() will give the same results (for the same inputs) across different Qt versions, even if qHash() for the element type would.

It bears repeating that the implementation of qHashRangeCommutative() - like the qHash() overloads offered by Qt - may change at any time. You must not rely on the fact that qHashRangeCommutative() will give the same results (for the same inputs) across different Qt versions, even if qHash() for the element type would.

void QHash::qSetGlobalQHashSeed(intnewSeed)

Manually setting the global QHash seed value should be done only for testing and debugging purposes, when deterministic and reproducible behavior on a QHash is needed. We discourage to do it in production code as it can make your application susceptible to algorithmic complexity attacks.

From Qt 5.10 and onwards, the only allowed values are 0 and -1. Passing the value -1 will reinitialize the global QHash seed to a random value, while the value of 0 is used to request a stable algorithm for C++ primitive types types (like int) and string types (QString, QByteArray).

The seed is set in any newly created QHash. See qHash about how this seed is being used by QHash.

If the environment variable QT_HASH_SEED is set, calling this function will result in a no-op.