Results 1 to 3 of 3

Thread: QMap, keys()

  1. #1
    Join Date
    Jul 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default QMap, keys()

    Hi,

    is there a way to get a list of keys of a QMap in the order they were inserted into the Map. I only need to retrieve the last inserted "key" and this code does not give me the wanted result because apparently QMap inserts the Items automatically in ascendant order.

    Qt Code:
    1. QStringList list = map.keys();
    2. if(list.size()>0)
    3. wanted = list.last();
    To copy to clipboard, switch view to plain text mode 

    Is there a way to solve this without having to change the map?

    thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMap, keys()

    If you need to know the last inserted key then you need to remember it when you insert. QMap keeps items in order to support its primary purpose: fast lookup by key in large sets of data. If you want strict insertion ordering then use a QList and only ever append.

    Without your actual requirements it is hard offer other possibilities.

  3. #3
    Join Date
    Jul 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMap, keys()

    thank you for your answer,
    I'am using it to save file names entered by the user as key, and related informations to those files as value. The file names are 1,2,3,4...,300.. and if the user wants to add a new file it's most likely going to be the last added file +1, so in this case 301. So my objective is to remind the user of his last added file. For this I'am using;
    Qt Code:
    1. QMap<QString, <T> >
    To copy to clipboard, switch view to plain text mode 
    I'am open to any suggestions.

    Thanks.

Similar Threads

  1. Replies: 1
    Last Post: 3rd August 2009, 12:44
  2. QtableWidget and hot keys
    By sawerset in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2008, 19:29
  3. How to identify Num pad keys
    By sanjayshelke in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2008, 17:43
  4. Using Short cut keys
    By joseph in forum Qt Programming
    Replies: 14
    Last Post: 18th March 2008, 19:34
  5. QHash with 2 keys?
    By MrGarbage in forum Qt Programming
    Replies: 5
    Last Post: 6th September 2007, 01:09

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.