Results 1 to 3 of 3

Thread: Set QHash and QMap value directly

  1. #1
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Set QHash and QMap value directly

    How to set QHash, QMultiHash, QMap and QMultiMap value directly ? For QList<int> I can do like this

    Qt Code:
    1. QList<int>() << 1 << 2 << 3;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Set QHash and QMap value directly

    You can't because QHash etc are key-value-based.

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Set QHash and QMap value directly

    You can't as QHash and QValue lack a method for inserting that returns a reference to the container. So you can't chain method calls.

    try
    Qt Code:
    1. template<typename KEY,typename VALUE>
    2. struct MapConstructor : public QMap<KEY,VALUE>
    3. {
    4. MapConstructor<KEY,VALUE>& insert(const KEY &key, const VALUE &value)
    5. {
    6. QMap<KEY,VALUE>::insert(key, value);
    7. return *this;
    8. }
    9. };
    10.  
    11. QMap<QString,int> aMap = MapConstructor<QString,int>().insert("hello",5).insert("world",5).insert("hell",4);
    To copy to clipboard, switch view to plain text mode 

    or you could take a look at Boost.Assign, so you don't have to reinvent the wheel.

Similar Threads

  1. QHash : Virtual memory not released
    By jbenoit in forum Qt Programming
    Replies: 16
    Last Post: 14th November 2009, 17:35
  2. Q_PROPERTY of type QMap
    By eclarkso in forum Qt Programming
    Replies: 11
    Last Post: 30th October 2009, 19:43
  3. QMap Problem with arguments.
    By ankurjain in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2006, 13:12

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.