PDA

View Full Version : QMap, keys()



mugi
28th August 2012, 02:29
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.


QStringList list = map.keys();
if(list.size()>0)
wanted = list.last();

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

thanks

ChrisW67
28th August 2012, 02:51
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.

mugi
28th August 2012, 11:37
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;

QMap<QString, <T> >
I'am open to any suggestions.

Thanks.