Results 1 to 2 of 2

Thread: pointer to nested QMap and QList

  1. #1
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: pointer to nested QMap and QList

    Hi all,

    I need to pass reference to: QMap<int,QList<int> > myMap
    to function that should fill it in, so I did:

    Qt Code:
    1. QMap<int,QList<int> > myMap;
    2. this->myFunction(..other_vars...,&myMap);
    To copy to clipboard, switch view to plain text mode 

    inside function
    Qt Code:
    1. void myFunction(..other_vars..,QMap<int,QList<int> > *myMap) {
    2. if(!myMap->contains(actor_type))
    3. myMap->insert(actor_type,QList<int>());
    4. if(!myMap[actor_type].contains(actor_id))
    5. myMap[actor_type].append(actor_id);
    6. }
    To copy to clipboard, switch view to plain text mode 

    there is problem in last line:
    error: 'class QMap<int, QList<int> >' has no member named 'append'

    so I think second to last line is also problematic, but QMap and QList both have function "contains".
    In short words, how to access QList elements within QMap when I passed reference to QMap ?
    I could use myMap->value(actor_type) - but this returns const

    best regards
    Marek


    Added after 11 minutes:


    sorry for this lame question,
    last two lines should be:
    [CODE]
    if(!(*myMap)[actor_type].contains(actor_id))
    (*myMap)[actor_type].append(actor_id);
    [CODE]
    then it works.

    cheers
    Marek
    Last edited by franki; 4th December 2013 at 11:42.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: pointer to nested QMap and QList

    You can:
    1) pass it by reference:
    Qt Code:
    1. QMap<int,QList<int> > myMap;
    2. this->myFunction(..other_vars...,myMap);
    3.  
    4. void myFunction(..other_vars..,QMap<int,QList<int> > & myMap) {
    5. if(!myMap.contains(actor_type))
    6. myMap.insert(actor_type,QList<int>());
    7. if(!myMap[actor_type].contains(actor_id))
    8. myMap[actor_type].append(actor_id);
    9. }
    To copy to clipboard, switch view to plain text mode 
    2) de-reference the map before using operator []:
    Qt Code:
    1. void myFunction(..other_vars..,QMap<int,QList<int> > *myMap) {
    2. if(!myMap->contains(actor_type))
    3. myMap->insert(actor_type,QList<int>());
    4. auto & map = *myMap;
    5. if(!map[actor_type].contains(actor_id))
    6. map[actor_type].append(actor_id);
    7. }
    To copy to clipboard, switch view to plain text mode 
    3) explicit call "operator []" method
    Qt Code:
    1. void myFunction(..other_vars..,QMap<int,QList<int> > * myMap) {
    2. if(!myMap->contains(actor_type))
    3. myMap->insert(actor_type,QList<int>());
    4. if(!myMap->operator[](actor_type).contains(actor_id))
    5. myMap->operator[](actor_type).append(actor_id);
    6. }
    To copy to clipboard, switch view to plain text mode 

    myMap[i] in your code is interpreted as indexed access to myMap array (remember the pointers <-> arrays relation), not as calling operator[] method on *myMap object.
    Last edited by stampede; 4th December 2013 at 11:55. Reason: typos

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

    franki (4th December 2013)

Similar Threads

  1. QMap in QList | i can get value but i can`t set value
    By petrusPL in forum Qt Programming
    Replies: 4
    Last Post: 20th March 2013, 18:40
  2. QMap only works when declared as a pointer
    By TheGrudge in forum Qt Programming
    Replies: 3
    Last Post: 10th February 2013, 10:12
  3. [solved] Qmap and pointer issue
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 26th September 2007, 14:34
  4. QMap in QList ?
    By npc in forum Newbie
    Replies: 2
    Last Post: 5th February 2007, 11:51
  5. Is there a Pointer Based QMap or Similar
    By vasudhamirji in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 14:34

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.