Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. creating a qHash function for QPointF
Forum Updated to NodeBB v4.3 + New Features

creating a qHash function for QPointF

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Boa Constructor
    wrote on 10 Apr 2023, 15:19 last edited by Boa Constructor 4 Oct 2023, 15:22
    #1

    Hi
    Ive created the hash table in a class

    QHash<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 specialization

    am I putting the function in the wrong place is my decleration incorect?

    I am using Qt 6.5

    C 1 Reply Last reply 10 Apr 2023, 15:28
    0
    • B Boa Constructor
      10 Apr 2023, 15:19

      Hi
      Ive created the hash table in a class

      QHash<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 specialization

      am I putting the function in the wrong place is my decleration incorect?

      I am using Qt 6.5

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 10 Apr 2023, 15:28 last edited by
      #2

      @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)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      B 2 Replies Last reply 10 Apr 2023, 15:32
      1
      • C Christian Ehrlicher
        10 Apr 2023, 15:28

        @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)

        B Offline
        B Offline
        Boa Constructor
        wrote on 10 Apr 2023, 15:32 last edited by Boa Constructor 4 Oct 2023, 15:54
        #3

        @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

        1 Reply Last reply
        0
        • C Christian Ehrlicher
          10 Apr 2023, 15:28

          @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)

          B Offline
          B Offline
          Boa Constructor
          wrote on 10 Apr 2023, 17:25 last edited by Boa Constructor 4 Oct 2023, 17:43
          #4

          @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());}
          };
          }
          
          C 1 Reply Last reply 10 Apr 2023, 18:18
          0
          • B Offline
            B Offline
            Boa Constructor
            wrote on 10 Apr 2023, 18:17 last edited by
            #5

            ended up doing a work around by making the hash key size_t and calling

            size_t LayerScene::hashPoint(const QPointF &key)
            {
                return qHashMulti(0, key.x(), key.y());
            }
            

            anytime i needed to refrence anything in the hashtable

            1 Reply Last reply
            0
            • B Boa Constructor
              10 Apr 2023, 17:25

              @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());}
              };
              }
              
              C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 10 Apr 2023, 18:18 last edited by Christian Ehrlicher 4 Oct 2023, 18:18
              #6

              I 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;
              }
              

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • B Boa Constructor has marked this topic as solved on 10 Apr 2023, 18:21

              1/6

              10 Apr 2023, 15:19

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved