Home > Mobile >  custom qHash method is not being called
custom qHash method is not being called

Time:11-17

I'm using QPoint as a key in a QHash, and following the documentation, I implemented a global qHash method for QPoint like so:

inline uint qHash(QPoint const &key, uint seed) {
  size_t hash = qHash(QPair<int, int>(key.x(), key.y()), seed);
  qDebug() << hash;
  return hash;
}

I'm using it like this

class HashTest {
  public:
    QHash<QPoint, QColor> hash;
    void addPixel(QPoint pt, QColor color) {
      hash[pt] = color;
    }
}

The insert still happens correctly, but it's not using my qHash function. Even if I comment out the qHash function, it still inserts. Considering that QPoint is documented as not having a qHash function, is this expected behavior?

EDIT: Minimal Reproducible Example

#include <QDebug>
#include <QGuiApplication>
#include <QHash>
#include <QPoint>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[]) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

  QHash<QPoint, int> hash;
  QPoint key = QPoint(0, 0);
  hash[key] = 3;
  qDebug() << hash[key]; // 3
}

CodePudding user response:

qHash for QPoint might not be documented but sure is defined:

Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept;
  • Related