Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QHash value result when key is not in hash
Qt 6.12 LTS is out! See what's new in the release blog

QHash value result when key is not in hash

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.5k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Publicnamer
    wrote on last edited by
    #1

    Looking at the documentation for QHash, it says that for some reason it will
    construct a default object when the key doesn't correspond to an item in the hash.
    But... what if I really just want it to return nullptr?
    Do I need to use a different container for that behavior?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • P Publicnamer

      Looking at the documentation for QHash, it says that for some reason it will
      construct a default object when the key doesn't correspond to an item in the hash.
      But... what if I really just want it to return nullptr?
      Do I need to use a different container for that behavior?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Publicnamer Do you mean operator[]?
      Simply use https://doc.qt.io/qt-5/qhash.html#value-1

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Publicnamer Do you mean operator[]?
        Simply use https://doc.qt.io/qt-5/qhash.html#value-1

        P Offline
        P Offline
        Publicnamer
        wrote on last edited by
        #3

        @jsulm
        Notice that it says:

        If the hash contains no item with the key, the function inserts a default-constructed value into the hash with the key, and returns a reference to it. If the hash contains multiple items with the key, this function returns a reference to the most recently inserted value.
        

        I want it to return nullptr when the item with my key isn't in the hash.

        jsulmJ 2 Replies Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          That's the single argument version documentation.

          @jsulm suggests the overload that takes a second default value argument.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • P Publicnamer

            @jsulm
            Notice that it says:

            If the hash contains no item with the key, the function inserts a default-constructed value into the hash with the key, and returns a reference to it. If the hash contains multiple items with the key, this function returns a reference to the most recently inserted value.
            

            I want it to return nullptr when the item with my key isn't in the hash.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Publicnamer Please spend a bit more time to read documentation properly...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • P Publicnamer

              Looking at the documentation for QHash, it says that for some reason it will
              construct a default object when the key doesn't correspond to an item in the hash.
              But... what if I really just want it to return nullptr?
              Do I need to use a different container for that behavior?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Publicnamer
              Although what @jsulm and @SGaist say about drawing your attention to const T QHash::value(const Key &key, const T &defaultValue) const is correct, it seems to me there is something wrong in your question.

              If you want to "return nullptr" that must mean your value type is a pointer, e.g. QHash<QString, Something*>, not QHash<QString, Something>. And if that is so the default constructed value (for const T QHash::value(const Key &key) const) should already be nullptr. If your hash is in fact QHash<QString, Something> you won't be able to return nullptr, it is not type-compatible.

              Is that not your case? If I am incorrect here I should be obliged if @jsulm or @SGaist corrects me.

              P.S.
              Be sure you use one of these QHash::value() methods, not the [] operator. The former return a value when not found but do not insert a new key-value into the hash, while the latter actually inserts a new key-value into the hash.

              1 Reply Last reply
              1
              • P Publicnamer

                @jsulm
                Notice that it says:

                If the hash contains no item with the key, the function inserts a default-constructed value into the hash with the key, and returns a reference to it. If the hash contains multiple items with the key, this function returns a reference to the most recently inserted value.
                

                I want it to return nullptr when the item with my key isn't in the hash.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Publicnamer said in QHash value result when key is not in hash:

                If the hash contains no item with the key, the function inserts a default-constructed value into the hash with the key, and returns a reference to it. If the hash contains multiple items with the key, this function returns a reference to the most recently inserted value.

                This is from https://doc.qt.io/qt-5/qhash.html#operator-5b-5d NOT from https://doc.qt.io/qt-5/qhash.html#value-1 !

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt
                • Unsolved