Results 1 to 9 of 9

Thread: QHash with key and struct problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QHash with key and struct problem

    Well, is it possible to get value of key if there is a value known from QHash?

    Ok, that was another stupid question, it is all in docs ... there is a key() method ...

    But, following method:
    Qt Code:
    1. inline qint16 merchandiseId(const QString& strKey) const
    2. { return m_strNames.key(strKey).iLanguageId; };
    To copy to clipboard, switch view to plain text mode 
    produces following errors:
    Qt Code:
    1. In file included from CMerchandizeElement.cpp:8:
    2. CMerchandizeElement.h: In member function `qint16 CMerchandizeElement::merchandiseId(const QString&) const':
    3. CMerchandizeElement.h:95: error: no matching function for call to `QHash<qint16, merchandiseLangStruct>::key(const QString&) const'
    4. c:/Qt/4.4.3/include/QtCore/../../src/corelib/tools/qhash.h:652: note: candidates are: const Key QHash<K, V>::key(const T&) const [with Key = qint16, T = merchandiseLangStruct]
    5. c:/Qt/4.4.3/include/QtCore/../../src/corelib/tools/qhash.h:658: note: const Key QHash<K, V>::key(const T&, const Key&) const [with Key = qint16, T = merchandiseLangStruct]
    6. mingw32-make[1]: *** [debug/CMerchandizeElement.o] Error 1
    7. mingw32-make: *** [debug] Error 2
    To copy to clipboard, switch view to plain text mode 
    Can someone help me please?
    Last edited by MarkoSan; 6th October 2008 at 22:16.
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash with key and struct problem

    Your hash is QHash<qint16, merchandiseLangStruct>, so the key() method needs to accept const merchandiseLangStruct&, and you are passing const QString&. You need to pass the struct itself.

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QHash with key and struct problem

    So what is the point then if I pass to this method whole struct, which already includes id?
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash with key and struct problem

    If you lookup by strMerchandiseName then they key should be QString.
    If you lookup by iLanguageId, then the key should be qInt16.
    If you lookup using both then you need 2 indexes. The index does not have to be QHash, it could be a simple sequential scan, but you need an index for every key.

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QHash with key and struct problem

    Quote Originally Posted by yuriry View Post
    If you lookup by strMerchandiseName then they key should be QString.
    If you lookup by iLanguageId, then the key should be qInt16.
    If you lookup using both then you need 2 indexes. The index does not have to be QHash, it could be a simple sequential scan, but you need an index for every key.
    Can you provide me please with some example?
    Qt 5.3 Opensource & Creator 3.1.2

  6. #6
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash with key and struct problem

    You already have QHash<qint16, merchandiseLangStruct>, use it to lookup by iLanguageId. Add another hash QHash<QString, merchandiseLangStruct> and use it to lookup by strMerchandiseName. Just be careful to update both hashes when you add/remove entries.

    If the number of entries not more than approximately 500, simple sequential scan might be even faster than a hash lookup. Instead of using two QHashes, just use a array or a list to store merchandiseLangStructs and add two methods that use foreach to find the right entry in the list. Then you don't have to synchronize two QHashes when you add/remove entries.

    PS: if you decide to go with two QHashes, consider storing merchandiseLangStruct* (pointers) instead of merchandiseLangStruct (values)
    Last edited by yuriry; 7th October 2008 at 17:34. Reason: reformatted to look better

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.