未调用自定义 qHash 方法
custom qHash method is not being called
我使用 QPoint 作为 QHash 中的键,并且按照文档,我为 QPoint 实现了一个全局 qHash 方法,如下所示:
inline uint qHash(QPoint const &key, uint seed) {
size_t hash = qHash(QPair<int, int>(key.x(), key.y()), seed);
qDebug() << hash;
return hash;
}
我是这样用的
class HashTest {
public:
QHash<QPoint, QColor> hash;
void addPixel(QPoint pt, QColor color) {
hash[pt] = color;
}
}
插入仍然正确,但它没有使用我的 qHash 函数。即使我注释掉 qHash 函数,它仍然会插入。考虑到 QPoint 被记录为没有 qHash 函数,这是预期的行为吗?
编辑:最小可重现示例
#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
}
QPoint 的 qHash 可能没有记录,但 sure is defined:
Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept;
我使用 QPoint 作为 QHash 中的键,并且按照文档,我为 QPoint 实现了一个全局 qHash 方法,如下所示:
inline uint qHash(QPoint const &key, uint seed) {
size_t hash = qHash(QPair<int, int>(key.x(), key.y()), seed);
qDebug() << hash;
return hash;
}
我是这样用的
class HashTest {
public:
QHash<QPoint, QColor> hash;
void addPixel(QPoint pt, QColor color) {
hash[pt] = color;
}
}
插入仍然正确,但它没有使用我的 qHash 函数。即使我注释掉 qHash 函数,它仍然会插入。考虑到 QPoint 被记录为没有 qHash 函数,这是预期的行为吗?
编辑:最小可重现示例
#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
}
QPoint 的 qHash 可能没有记录,但 sure is defined:
Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept;