PDA

View Full Version : Problem With QMap



alizadeh91
10th March 2013, 06:42
Hi guys.
I have ordering problem with QMap. I've googled it but my problem didn't solved.
Suppose I have QMap<QString,int>. After i insert some values into QMap the ordering is not kept. I didn't find any substitution option.
Is there any (key, value) pairs container class int Qt that keep the ordering of keys.

alizadeh91
10th March 2013, 11:00
I've solved it by this way : QList<QPair<T1,T2> > and that’s ok :)

anda_skoa
10th March 2013, 11:12
Can you give an example of keys you insert and order you expect to get?

QMap should be sorted by keys, more precisely sorted by operator<() of the key type.

Cheers,
_

alizadeh91
10th March 2013, 11:40
I want to key be sorted as insertion

amleto
10th March 2013, 23:07
thats what happens in qmap.

ChrisW67
11th March 2013, 02:51
A QMap is not a list, it is a collection of mappings between a value of one type and a value of another. The keys of the QMap are kept in sorted order by value, which has nothing to do with the order of insertion. A QMap can have only one key of a given value and subsequent insertions with the same key will replace the existing map entry (contrast that with QMultiMap).

Your solution with QList and QPair is one option. Another would be a QList of structure, which allows you to chose meaningful field names rather than use QPair's "first" and "second").


struct MyData {
QString stringValue;
int someIntegerValue;
};
QList<MyData> list;

alizadeh91
11th March 2013, 09:51
Yeah I know :) thanks for all :)
I've used : QList< QPair<T1,T2> > . Is this right way?

ChrisW67
12th March 2013, 08:14
Yes, it is a way.