Results 1 to 8 of 8

Thread: QHash ordered by value

  1. #1
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QHash ordered by value

    Hi all,
    I'd need to have a QHash where the elements are ordered by a certain filed inside the value.

    Qt Code:
    1. struct MyData
    2. {
    3. MyData(): bitmap(0), weight(0) {}
    4. MyData(qint64 b, qint32 w) : bitmap(b), weight(w) {}
    5.  
    6. qint64 bitmap;
    7. qint32 weight;
    8. };
    9.  
    10. QHash<qint32, MyData> mCurrentCache;
    To copy to clipboard, switch view to plain text mode 

    In my case I would like to order the elements depending on MyData::weight.
    Is there a smart way to do that or do I have to resort to an additional QHash<qint32, qint32> as a look up table?

    Thanks for any help
    bye

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash ordered by value

    1. QHash is not a data structure that can be sorted.
    2. Why don't you use the weight as a key? Like this:
    Qt Code:
    1. mCurrentCache[myDataObject.weight] = myData;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash ordered by value

    Also, you need to declare MyData as a qt metatype, to be able to use it in a template container.
    Qt Code:
    1. Q_DECLARE_METATYPE(MyStruct)
    To copy to clipboard, switch view to plain text mode 

    Add this right after the struct definition.

  4. #4
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash ordered by value

    Thanks for the quick reply.
    I don't use the "weight" as a key because I need the key for other purposes.
    The other thing I could do is to use a QMap<weight, key> knowing that the insert there preserve the order. But I though there was a smarter way to have only one QHash.
    bye

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash ordered by value

    You could create another class to be used as a key, just like in the QHash example:
    Qt Code:
    1. class MyKey
    2. {
    3. public:
    4. MyKey();
    5. MyKey(const qint32& key, const qint32& weight);
    6.  
    7. //add == operator
    8. //add uint qHash(const MyKey& k);
    9. private:
    10. qint32 weight;
    11. qint32 key;
    12. };
    To copy to clipboard, switch view to plain text mode 


    You could then use MyKey as a QHash key.

  6. #6
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash ordered by value

    Interesting, would it be enought to redefine the operator > to order by the weight filed?
    The only problem is that with this I won't have a fast look up on the key, probably I could redefine even the contains method and the [] operator.....

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash ordered by value

    As I told you before, QHash is not sorted, so implementing the > or < operators would make no sense.

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QHash ordered by value

    Why not a single map:
    Qt Code:
    1. QMap<qint32, qint64> bitmaps;
    To copy to clipboard, switch view to plain text mode 
    where key is weight and value is bitmap identifier or whatever is it?
    J-P Nurmi

Similar Threads

  1. QHash static member error
    By ramazangirgin in forum Qt Programming
    Replies: 7
    Last Post: 5th February 2008, 11:26
  2. QHash with 2 keys?
    By MrGarbage in forum Qt Programming
    Replies: 5
    Last Post: 6th September 2007, 01:09
  3. signals and slots with container types like QHash
    By themolecule in forum Qt Programming
    Replies: 3
    Last Post: 28th August 2007, 08:07
  4. Query about QHash , QList
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2006, 09:04
  5. about QHash elements order
    By bruce1007 in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2006, 07:17

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.