Results 1 to 9 of 9

Thread: QHash with key and struct problem

  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 QHash with key and struct problem

    Hi to all!

    In my code I have:
    Qt Code:
    1. typedef struct merchandiseLangStruct
    2. {
    3. qint16 iLanguageId; // lang id
    4. QString strMerchandiseName; // language name
    5. };
    6.  
    7. ...
    8. ...
    9. ...
    10.  
    11. class A
    12. {
    13. ...
    14. ...
    15. QHash<qint16, merchandiseLangStruct> m_strNames; // multilanguages names with lang. ids as keys
    16. ...
    17. ...
    18.  
    19. public:
    20. inline QString name(const qint16& iId) const
    21. { return m_strNames.value(iId).m_strMerchandiseName; };
    22. };
    To copy to clipboard, switch view to plain text mode 
    If I try to compile this .h file, I get:
    Qt Code:
    1. CMerchandizeElement.h: In member function `QString CMerchandizeElement::name(const qint16&) const':
    2. CMerchandizeElement.h:91: error: 'const struct merchandiseLangStruct' has no member named 'm_strMerchandiseName'
    3. mingw32-make[1]: *** [debug/CMerchandizeElement.o] Error 1
    4. mingw32-make[1]: Leaving directory `C:/Documents and Settings/markofr/Desktop/erosNewCheckout/trunk/Client'
    5. mingw32-make: *** [debug] Error 2
    To copy to clipboard, switch view to plain text mode 
    What I am trying to do is to get a string from qhash stored inside struct. How do I do that, please help, I am sure it is some form of stupid mistake but I am getting desperate ...
    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

    The variable name is strMerchandiseName and you are trying to get m_strMerchandiseName

  3. The following user says thank you to yuriry for this useful post:

    MarkoSan (6th October 2008)

  4. #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

    Oh, Jesus, stupid me, god damn, sorry for this garbage thread ...
    Qt 5.3 Opensource & Creator 3.1.2

  5. #4
    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 23:16.
    Qt 5.3 Opensource & Creator 3.1.2

  6. #5
    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.

  7. #6
    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

  8. #7
    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.

  9. #8
    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

  10. #9
    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 18: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.