我使用 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
}
uj5u.com熱心網友回復:
QPoint 的 qHash 可能沒有記錄,但肯定定義了:
Q_CORE_EXPORT size_t qHash(QPoint key, size_t seed = 0) noexcept;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/359382.html
