Results 1 to 5 of 5

Thread: QMap Issue

  1. #1

    Unhappy QMap Issue

    I have QMap<MyClass*, bool> and I need to remove the oldest item. Can someone provide the code to do it?

    I tried removing last Key and first Key, but it still doesn't work properly.

    Does it sort the map somehow? And if it does, is there a way to prevent it.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QMap Issue

    Quote Originally Posted by ManuMies View Post
    Does it sort the map somehow? And if it does, is there a way to prevent it.
    http://doc.qt.nokia.com/4.7/qmap.html

  3. #3

    Default Re: QMap Issue

    Thanks, so QHash is answer to my problem. Solved.


    Added after 1 12 minutes:


    Nope, that didn't work. Now the list is indeed in random order, but it's not any better.
    Last edited by ManuMies; 9th November 2010 at 11:51.

  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: QMap Issue

    Use an additional QList to keep the order of insertions. Or use QCache
    Last edited by wysota; 9th November 2010 at 12:23.
    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. #5
    Join Date
    Nov 2010
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QMap Issue

    What do you mean by "oldest item" ?
    Do you want to remove the first item that was added to the map ? If so a map is not a good choice for that, as it does not track the order of the elements, and in fact the order is random and can change any time.
    You can use a vector instead or in addition to the map. When adding a pointer to the map also add it to the vector. Then you would take the last element of the vector, find that element in the map and remove both the element form the map and from the vector.
    It is a double penalty in terms of memory and time but I am afraid there is no way around that ( unless you do not need the map at all ).
    Last edited by Vadim; 11th November 2010 at 00:39.

Similar Threads

  1. QMap
    By sophister in forum Qt Programming
    Replies: 5
    Last Post: 25th May 2009, 10:05
  2. QMap
    By AnithaRagupathy in forum Qt Programming
    Replies: 5
    Last Post: 23rd November 2007, 13:26
  3. Qmap
    By phillip_Qt in forum Qt Programming
    Replies: 3
    Last Post: 23rd November 2007, 10:43
  4. [solved] Qmap and pointer issue
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 26th September 2007, 14:34
  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.