Questions tagged [qhash]

QHash is a Qt template class that provides a hash-table-based dictionary

QHash provides very similar functionality to QMap. The differences are:

  • QHash provides faster lookups than QMap.
  • When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.
  • The key type of a QMap must provide operator<(). The key type of a QHash must provide operator==() and a global hash function called qHash() (see qHash).

Documentation can be found here for Qt 4.8 and here for Qt 5.

68 questions
7
votes
2 answers

Built-in 64-bit hash function for QString?

qHash(const QString&) returns uint, which is 32-bit. Is there any standard Qt way of getting 64-bit hash for a string on 32-bit system? Or do I have to implement a hash function myself?
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
6
votes
2 answers

How can I update the value of QHash for a specific key?

I am using QHash in C++ to store some simple key and value pairs. In my case the key is an integer, so is the value. To add a new key/value pair to the hash, this is my syntax: QHash myhash; int key = 5; int value =…
panofish
  • 7,578
  • 13
  • 55
  • 96
5
votes
3 answers

loop QHash by insert order

Is it possible to loop a QHash by the insert order? The method below seem to loop the hash by some other factor: QHashIterator i(hash); while (i.hasNext()) { i.next(); qDebug() << i.key() << ": " << i.value(); } EDIT: I…
Dennis
  • 3,448
  • 8
  • 38
  • 45
4
votes
2 answers

Should I use qAsConst on QHash::keys() in a C++11 range-based for

In this article Goodbye, Q_FOREACH from KDAB, they warn that a range-based for could cause a detach of a Qt container. See also here : Using C++11 range-based for loop correctly in Qt I understand that the for will cause a detach because it is…
ymoreau
  • 3,402
  • 1
  • 22
  • 60
4
votes
1 answer

Understanding what QHash does when key not found

Note: You can find a minimal working example at the end of this post. I'm using Qt 5.7. Let's say I have the following QHash: QHash hm; with enum HashKey { K1, K2, K3, K4, K5 } and class HashValue { …
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
4
votes
1 answer

qvariant as key in qhash

I want to create a data structure with QVariants a keys. It looks like this: QHash, SHAPES::Shape* > _shapes; Unfortunately there is "no matching function for call to ‘qHash(const QVariant&)’". So I defined my own…
Stefan Ramson
  • 531
  • 1
  • 6
  • 18
4
votes
1 answer

Qt4 QHash hash collision?

I am using QT 4.8 and I notice that it has a QHash class which can be used as follows: QHash hash; hash["one"] = 1; hash["three"] = 3; hash["seven"] = 7; hash.insert("twelve", 12); If there is a hash collision, will it be…
mzzhmh
  • 79
  • 9
4
votes
1 answer

Implementing a QHash-like lookup with multiple keys

I'm trying to find the best way to implement a QHash-like lookup table that uses multiple keys to return one value. I have read that the Boost library has similar functionality, but I would like to avoid this if possible. An example of what I would…
dajaffe
  • 855
  • 13
  • 34
3
votes
2 answers

QList/QHash store abstract elements

I'd like to store in QHash elements that inherits from one class. So I've got: class ImageInterface { public: ImageInterface(); ImageInterface(const QString& path); virtual QString getName() const = 0; }; And implementation: class Image…
Mikooos
  • 5,360
  • 5
  • 31
  • 45
3
votes
1 answer

Initialize QHash inside a class

I want to initialize a QHash<...> inside a class. There is no problem, if the code is compiled with gcc on linux. But if I use MSVC12, I get the following error: C2661: QHash<...>::QHash:no overloaded function takes X parameters Minimal…
NelDav
  • 785
  • 2
  • 11
  • 30
3
votes
1 answer

Correct way to call a member function via a QHash with member function pointers

I have a Server class that processes QJsonObject data and handles it according to a key set in the data. At the moment, I use a big if-then-else statement to decide what to do like this: const QString action =…
Tobias Leupold
  • 1,512
  • 1
  • 16
  • 41
3
votes
2 answers

QSet: error with qHash and custom class

I want to create a QSet that I want to use inside my program. Tag is my custom class. When I build the code I obtain gcc errors regarding the qHash overloading: Exercise.cpp.o In file included from…
Jepessen
  • 11,744
  • 14
  • 82
  • 149
3
votes
1 answer

Constantly generating QPixmap* fails after a number of iterations

I am using the following code in order to generate QPixmap* pointers and then insert them into QHash (I will show only the pointers generation code since this is the one that fails). QPixmap* MyClass::loadImg(QString fileName) { …
Soc
  • 283
  • 3
  • 17
3
votes
1 answer

How to use Qmap inside a Qhash?

I have to create a QHash with a map QMap inside it, I have tried to write it as…
CowboY
  • 99
  • 2
  • 7
3
votes
1 answer

QHash of QVectors

I have a QHash > qhash ,and trying to overwite the values in QVector as following: void Coordinate::normalizeHashElements(QHash > qhash) { string a = "Cluster"; float new_value; float…
SuTron
  • 387
  • 5
  • 16
1
2 3 4 5