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. Reverse lookup key by value for QHash<int,QByteArray>

Reverse lookup key by value for QHash<int,QByteArray>

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 906 Views 2 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    I am trying to get the keys from a QHash<int,QByteArray> object. Is there a predefined function that does this or is this just a search through list every time kind of thing? The list is not long, but if there is a function to do this I would rather use that. Like some kind of q<helper> function. I know this is done in views already, but I am not sure where.

    C++ is a perfectly valid school of magic.

    JonBJ 1 Reply Last reply
    0
    • fcarneyF fcarney

      I am trying to get the keys from a QHash<int,QByteArray> object. Is there a predefined function that does this or is this just a search through list every time kind of thing? The list is not long, but if there is a function to do this I would rather use that. Like some kind of q<helper> function. I know this is done in views already, but I am not sure where.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @fcarney

      I am trying to get the keys from a QHash

      I must be misunderstanding? QList<Key> QHash::keys() const, https://doc.qt.io/qt-5/qhash.html#keys?

      EDIT OIC, I need to look at title, sorry...!

      Then do you want const Key QHash::key(const T &value) const, https://doc.qt.io/qt-5/qhash.html#key

      Returns the first key mapped to value.
      [...]
      This function can be slow (linear time),

      1 Reply Last reply
      3
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        @fcarney said in Reverse lookup key by value for QHash<int,QByteArray>:

        I am trying to get the keys from a QHash<int,QByteArray> object. Is there a predefined function that does this or is this just a search through list every time kind of thing?

        Given the type of underlying data structure that hashes come from, it is a linear search every time you do the lookup (negating the benefits of the hash) when you use
        QList<Key> QHash::keys(const T &value) const

        If you meet the AI on the road, kill it.

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by fcarney
          #4

          I don't think I explained very well. I have the "value" and am wondering if there was a predefined way to get the "key" from the "value". Otherwise I will just search the list for a match and return an error if not found. Qt has a lot of prebuilt stuff and I was wondering if they had something for this already.

          Edit:
          @JonB
          Doh, I totally missed your explanation of the key function! Thanks! I looked at the class definition multiple times and did not see it either!

          C++ is a perfectly valid school of magic.

          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 Extensions
          • Unsolved