3

I tried using this code:

QPoint pt;
QHash<QPoint, double> hexes;
hexes.contains(pt);

and I get the error:

error: no matching function for call to ‘qHash(const QPoint&)’

But if I replace 'QPoint' with 'int' it compiles just fine. Is it possible to use QPoint as a QHash key type?

user2602914
  • 301
  • 3
  • 11

2 Answers2

5

The qHash hashing function is not defined for QPoint, but you can write one using QPair<int,int>

inline uint qHash (const QPoint & key)
{
    return qHash (QPair<int,int>(key.x(), key.y()) );
}
galinette
  • 8,896
  • 2
  • 36
  • 87
2

Is it possible to use QPoint as a QHash key type?

According to documentation it's possible. But you need define:

inline uint qHash (const QPoint & key)  

Also QHash requires:

inline bool operator== (const QPoint & k1, const QPoint & k2)  

but it's already defined.

Since qHash is already implemented for 64-bit integers I believe that this solution has sense:

inline uint qHash (const QPoint & key)
{
    return qHash (static_cast <qint64> (key.x () ) << 32 | key.y () );
}

Note: I don't know how to implement correct hash function which based on two 32-bit digits and output 32-bit hash.

Gluttton
  • 5,739
  • 3
  • 31
  • 58