creating a qHash function for QPointF
-
wrote on 10 Apr 2023, 15:19 last edited by Boa Constructor 4 Oct 2023, 15:22
Hi
Ive created the hash table in a classQHash<QPointF,PointControl*> pointControlsHashTable
and the qHash function in main.cpp
inline size_t qHash(const QPointF &key,size_t seed) { return qHashMulti(seed, key.x(), key.y()); }
this way a QPointF should be a valid QHash key since it now has a qHash function and a == operator
however I am still getting there error:
error: static assertion failed: The key type must have a qHash overload or a std::hash specializationam I putting the function in the wrong place is my decleration incorect?
I am using Qt 6.5
-
Hi
Ive created the hash table in a classQHash<QPointF,PointControl*> pointControlsHashTable
and the qHash function in main.cpp
inline size_t qHash(const QPointF &key,size_t seed) { return qHashMulti(seed, key.x(), key.y()); }
this way a QPointF should be a valid QHash key since it now has a qHash function and a == operator
however I am still getting there error:
error: static assertion failed: The key type must have a qHash overload or a std::hash specializationam I putting the function in the wrong place is my decleration incorect?
I am using Qt 6.5
@Boa-Constructor said in creating a qHash function for QPointF:
Hi
Ive created the hash table in a class...
and the qHash function in main.cpp
And how should the compiler know that you have a qHash function somewhere in your sources? The compiler needs to see the function when you want to use it (like any other function too)
-
@Boa-Constructor said in creating a qHash function for QPointF:
Hi
Ive created the hash table in a class...
and the qHash function in main.cpp
And how should the compiler know that you have a qHash function somewhere in your sources? The compiler needs to see the function when you want to use it (like any other function too)
wrote on 10 Apr 2023, 15:32 last edited by Boa Constructor 4 Oct 2023, 15:54@Christian-Ehrlicher how should I go about showing it where it is?
sorry if this is a dumb question realtive new to Qt and C++main.cpp is in the PROJECT_SOURCES
ive tried putting it in the classes and the header where the QHash is used -
@Boa-Constructor said in creating a qHash function for QPointF:
Hi
Ive created the hash table in a class...
and the qHash function in main.cpp
And how should the compiler know that you have a qHash function somewhere in your sources? The compiler needs to see the function when you want to use it (like any other function too)
wrote on 10 Apr 2023, 17:25 last edited by Boa Constructor 4 Oct 2023, 17:43@Christian-Ehrlicher
ive tried both these in header files outside of classes like in the documantion https://doc.qt.io/qt-6/qhash.html#qhash but I am still geting the same error
The key type must have a qHash overload or a std::hash specialization
is this not what this code dos?inline size_t qHash(const QPointF &key,size_t seed) { return qHashMulti(seed, key.x(), key.y()); }
namespace std { template <> struct hash<QPointF> { // seed is optional size_t operator()(const QPointF &key, size_t seed) const {return qHashMulti(seed, key.x(), key.y());} }; }
-
wrote on 10 Apr 2023, 18:17 last edited by
ended up doing a work around by making the hash key
size_t
and callingsize_t LayerScene::hashPoint(const QPointF &key) { return qHashMulti(0, key.x(), key.y()); }
anytime i needed to refrence anything in the hashtable
-
@Christian-Ehrlicher
ive tried both these in header files outside of classes like in the documantion https://doc.qt.io/qt-6/qhash.html#qhash but I am still geting the same error
The key type must have a qHash overload or a std::hash specialization
is this not what this code dos?inline size_t qHash(const QPointF &key,size_t seed) { return qHashMulti(seed, key.x(), key.y()); }
namespace std { template <> struct hash<QPointF> { // seed is optional size_t operator()(const QPointF &key, size_t seed) const {return qHashMulti(seed, key.x(), key.y());} }; }
Lifetime Qt Championwrote on 10 Apr 2023, 18:18 last edited by Christian Ehrlicher 4 Oct 2023, 18:18I see that qHash(QPointF) was explictily deleted in with this commit: https://codereview.qt-project.org/c/qt/qtbase/+/336743 and I agree with this decision. The compiler also tells you about this.
But std::hash overload still works (but has the same flaw as qHash):
#include <QHash> #include <QPointF> namespace std { template <> struct hash<QPointF> { // seed is optional size_t operator()(const QPointF &key, size_t seed) const {return qHashMulti(seed, key.x(), key.y());} }; } int main(int argc, char *argv[]) { QHash<QPointF,int> pointControlsHashTable; pointControlsHashTable[QPointF(0,0)] = 25; return 0; }
-
1/6