Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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? 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.



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



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



  • 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!


Log in to reply