我使用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
}发布于 2021-11-17 08:46:53
qHash for QPoint可能没有文档化,但确定是定义的
Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept;https://stackoverflow.com/questions/70000687
复制相似问题