Results 1 to 6 of 6

Thread: Multiple values per key in QMap

  1. #1
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple values per key in QMap

    Hello Qt Forum

    I recently researched about QMap a bit and I have a question.
    Is it possible to make a QMap ( or any other container ) that you can assign multiple values per key?

    for example:

    Qt Code:
    1. QMap<QString, int, int, double>
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple values per key in QMap

    Take a look at QMultiMap. You can use QVariant as the value type.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple values per key in QMap

    I already had a look at it, but it seemed the only difference is it doesn't overwrite existing keys with the same name?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple values per key in QMap

    This difference is enough to suit your needs. An alternative is this:

    Qt Code:
    1. struct SomeStruct {
    2. int v1;
    3. int v2;
    4. QString v3;
    5. };
    6.  
    7. typedef QMap<QString,SomeStruct> MyMap;
    To copy to clipboard, switch view to plain text mode 

    This is a better equivalent to the code from your post but less practical in my opinion.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    xander333 (16th November 2010)

  6. #5
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple values per key in QMap

    Thanks Wysota,

    but how would I set their values?
    not like insert(mymap, SomeStruct) .
    Because then the variables wouldn't be initialized right?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple values per key in QMap

    Uninitialized variables is not a problem as you can have constructors for your stuct but it might be a good idea to provide a method for the map that allows to set each value of the struct separately. But I think the solution with multimap is better in most cases as the only "trick" you need to do there is to convert QVariant to proper types when retrieving data from the map.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    xander333 (16th November 2010)

Similar Threads

  1. Replies: 1
    Last Post: 12th April 2010, 13:14
  2. How to get multiple lines values in XML file..
    By dark1988 in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2008, 08:24
  3. QMap
    By AnithaRagupathy in forum Qt Programming
    Replies: 5
    Last Post: 23rd November 2007, 13:26
  4. Replies: 0
    Last Post: 21st December 2006, 11:48
  5. Reg - QMap(Qt3.3.4)
    By suresh in forum Newbie
    Replies: 3
    Last Post: 18th October 2006, 22:04

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.